본문 바로가기

IT Trend

EchoThreadServer 분석 소스

 
import java.net.*;				
import java.io.*;				
				
public class EchoThreadServer {				
				
	public static void main(String[] args) {			
		try{		
			ServerSocket server = new ServerSocket(10001);	
			System.out.println("접속을 기다립니다.");	
			while(true){	
				Socket sock = server.accept();
				EchoThread echothread = new EchoThread(sock);
				echothread.start();
			} // while	
		}catch(Exception e){		
			System.out.println(e);	
		}	
	} // main		
}			
			
class EchoThread extends Thread{			
	private Socket sock;		
	public EchoThread(Socket sock){		
		this.sock = sock;	
	}		
	public void run(){		
		try{	
			InetAddress inetaddr = sock.getInetAddress();
			System.out.println(inetaddr.getHostAddress() + " 가 접속하였습니다.");
			OutputStream out = sock.getOutputStream();
			InputStream in = sock.getInputStream();
			PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
			BufferedReader br = new BufferedReader(new InputStreamReader(in));	
			String line = null;	
			while((line = br.readLine()) != null){	
				System.out.println("클라이언트가 보낸 문자열 : " + line);
				pw.println(line);
				pw.flush();
			}	
			pw.close();	
			br.close();	
			sock.close();	
		}catch(Exception ex){		
			System.out.println(ex);	
		}		
	} // run			
}				

'IT Trend' 카테고리의 다른 글

ChatServer 분석 소스  (0) 2016.12.12
ChatClient 분석 소스  (0) 2016.12.12
EchoServer 분석소스  (0) 2016.12.12
EchoClient 분석소스  (0) 2016.12.12
Chat 서비스 분석  (0) 2016.12.12