package src;
import java.io.*;
import java.net.Socket;
public class FileSocketClient {
class ClientSocket {
private Socket socket = null;
private BufferedOutputStream bout = null;
private BufferedInputStream bin = null;
private BufferedWriter bw = null;
public ClientSocket( File file )
{
try{
// File tmpFile = null;
// DataInputStream dis = null;
// DataOutputStream dos = null;
//[참고] 프로퍼티 파일로부터 읽어오도록 설정하여 사용!
String server_ip = "127.0.0.1";
int FILE_SERVER_PORT = 10001;
socket = new Socket( server_ip , FILE_SERVER_PORT ); //서버 ip 주소, 포트 세팅
//파일저장 경로를 가지고 오기위한 임시 파일 생성
bw = new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())); //파일명을 보냄
bw.write( file.getName() +"\n");
bw.flush();
System.out.println(" [Client] : 파일명 [ " + file.getName() + " ] 파일 보내기 시작.. " );
bin = new BufferedInputStream( new DataInputStream( new FileInputStream( file.getPath() ) ));
bout = new BufferedOutputStream( new DataOutputStream( socket.getOutputStream() ));
}catch( Exception e )
{
try{
/* 자원정리 */
if( bw != null) bw.close();
if( bin != null) bin.close();
if( bout != null) bout.close();
if( socket != null) socket.close();
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void running(){
try{
int b=0;
while( ( b= bin.read()) != -1 )
{
//sleep(1);
bout.write((char)b);
System.out.println((char)b);
//bout.flush();
//bout.flush();
//bout.flush();
//bout.flush();
}
System.out.println(" [Client] : " + "파일 보내기 완료" );
}catch( Exception e)
{
e.printStackTrace();
}
finally{
try{
/* 자원정리 */
bin.close();
bout.close();
socket.close();
System.out.println("전송끝");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
public void send( File file )
{
ClientSocket cs = new ClientSocket( file );
cs.running();
}
/* 메인 */
public static void main(String args[]) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
File file1 = new File("C:\\television.xml");
File file2 = new File("C:\\dvd_player.xml");
File file3 = new File("C:\\living_room.xml");
System.out.println("Select one");
System.out.println("1.TV 2 DVD 3.Living room 4.All");
System.out.print("Input : ");
String sel = in.readLine();
if(sel.equalsIgnoreCase("1")){
new FileSocketClient().send(file1);
}
else if(sel.equalsIgnoreCase("2")){
new FileSocketClient().send(file2);
}
else if(sel.equalsIgnoreCase("3")){
new FileSocketClient().send(file3);
}
else if(sel.equals("4")){
new FileSocketClient().send(file1);
Thread.sleep(3000);
new FileSocketClient().send(file2);
Thread.sleep(3000);
new FileSocketClient().send(file3);
}
}
}