import java.net.*; import java.io.*; public class SocketServer extends Thread { private ServerSocket ss; private Socket s; private InputStream is; private OutputStream os; private DataInputStream dis; private DataOutputStream dos; public SocketServer(){ try{ ss = new ServerSocket(5000); pln("서버 대기중(5000포트)...."); s = ss.accept(); pln("Socket 연결완료 : " + s); is = s.getInputStream(); os = s.getOutputStream(); dis = new DataInputStream(is); dos = new DataOutputStream(os); start(); read(); }catch(Exception e){ pln("Socket 생성시 예외" + e); }finally{ closeAll(); } } private void read(){ String msg = ""; try{ while(true){ msg = dis.readUTF(); pln("안드로이드 client Msg : " + msg); } }catch(IOException ie){ pln("메세지 읽는 예외 : " +ie); } } BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//Data Source public void run(){ // 쓰레드가 하는 일 String str = null; try{ while((str = br.readLine()) != null){ dos.writeUTF(str); //dos : Data Destination dos.flush(); } }catch(IOException ie){ pln("run() : " + ie.toString()); } } private void closeAll(){ try{ if(dis != null) dis.close(); if(dos != null) dos.close(); if(is != null) is.close(); if(os != null) os.close(); if(s != null) s.close(); if(ss != null) ss.close(); }catch(IOException ie){} } private void pln(String str){ System.out.println(str); } public static void main(String[] args) { new SocketServer(); } }