중개서버(BroadCastServer)가 Mp3Server에서 보내주는 파일을 EndUser에게 건네주는 방식
package io3;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class BroadcastServer {
ServerSocket mainServer;
Map<String, String> ipMap;
public BroadcastServer()throws Exception{
ipMap = new HashMap<String, String>();
ipMap.put("윤호","192.168.0.73");
ipMap.put("은애","192.168.0.85");
mainServer = new ServerSocket(8080);
System.out.println("ready......main ");
waitEndUser();
}
private void waitEndUser() {
Socket endUser = null;
InputStream in = null;
OutputStream out = null;
Scanner scanner = null;
byte[] buffer = new byte[1024*8];
try{
endUser = mainServer.accept();
System.out.println("EndUser: " + endUser);
in = endUser.getInputStream();
out = endUser.getOutputStream();
scanner = new Scanner(in);
System.out.println("1...................");
String serverName = scanner.nextLine();
System.out.println("2.....................");
String targetIp = ipMap.get(serverName);
Socket mp3Socket = new Socket(targetIp,5555);
InputStream mp3In = mp3Socket.getInputStream();
System.out.println("mp3In" +mp3In);
while(true){
int mp3Count = mp3In.read(buffer);
if(mp3Count == -1){ break;}
out.write(buffer,0, mp3Count);
}
out.flush();
mp3In.close();
mp3Socket.close();
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}finally{
try{ scanner.close();}catch(Exception e){}
try{ in.close();}catch(Exception e){}
try{ out.close();}catch(Exception e){}
try{ endUser.close();}catch(Exception e){}
}
waitEndUser();
}
public static void main(String[] args)throws Exception {
BroadcastServer server = new BroadcastServer();
}
}
---------------------------------------------------------------------------------------------
package io3;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class EndUser {
public void downloadMP3(){
String serverName ="은애\n";
String serverIp = "127.0.0.1";
int port = 8080;
Socket socket = null;
OutputStream out = null;
InputStream in = null;
FileOutputStream fos = null;
byte[] buffer = new byte[1024*8];
try{
socket = new Socket(serverIp, port);
out = socket.getOutputStream();
in = socket.getInputStream();
fos = new FileOutputStream("C:\\zzz\\aaa.mp3");
out.write(serverName.getBytes());
out.flush();
while(true){
int count = in.read(buffer);
if(count == -1){ break;}
System.out.println(count);
fos.write(buffer,0,count);
}
fos.flush();
fos.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{ out.close();}catch(Exception e){}
try{ socket.close();}catch(Exception e){}
}
}
public static void main(String[] args)throws Exception {
EndUser obj = new EndUser();
obj.downloadMP3();
}
}
---------------------------------------------------------------------------------------------
package io3;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Mp3Server {
private ServerSocket serverSocket;
public Mp3Server()throws Exception{
serverSocket = new ServerSocket(5555);
System.out.println("ready.....");
waitClient();
}
private void waitClient() {
System.out.println("클라이언트의 연결을 대기중...");
Socket client = null;
OutputStream out = null;
InputStream in = null;
byte[] buffer = new byte[1024*8];
try{
client = serverSocket.accept();
System.out.println(client);
out = client.getOutputStream();
in = new FileInputStream("C:\\zzz\\aaa.mp3");
while(true){
int count = in.read(buffer);
if(count == -1) { break; }
out.write(buffer,0, count);
}
out.flush();
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
try {in.close();} catch (Exception e) {}
try {out.close();} catch (Exception e) {}
try {client.close();} catch (Exception e) {}
}
waitClient();
}
public static void main(String[] args)throws Exception {
Mp3Server obj = new Mp3Server();
}
}
'코딩 > JAVA' 카테고리의 다른 글
(3) Java 자바 Mp3 파일 전송 서버 및 Key Annotation 예제 (Mp3Client) (0) | 2014.09.13 |
---|---|
(2) Java 자바 파일 복사 전용 함수(CopyUtil) (0) | 2014.09.13 |
(1) Java 자바 Template Method Pattern - ClientAgent (0) | 2014.09.13 |
Java 자바 DataInputStream & DataOutputStream 간단한 예제 (0) | 2014.09.13 |
Java 동적 로딩 (0) | 2014.09.12 |