root/trunk/24.SocketTest/assets/SocketServer.java

리비전 87, 1.7 kB (mefour에 의해 체크인됨, 15 년 전)

--

Line 
1 import java.net.*;
2 import java.io.*;
3
4 public class SocketServer extends Thread 
5 {
6         private ServerSocket ss;
7         private Socket s;
8         private InputStream is;
9         private OutputStream os;
10         private DataInputStream dis;
11         private DataOutputStream dos;
12
13         public SocketServer(){
14                 try{
15                         ss = new ServerSocket(5000);
16             pln("��� ��린以�5000�ы�)....");
17      
18                         s = ss.accept();     
19                         pln("Socket �곌껐��� : " + s);
20                        
21                         is = s.getInputStream();
22                         os = s.getOutputStream();
23                         dis = new DataInputStream(is);
24                         dos = new DataOutputStream(os);
25                         start();
26
27             read();                     
28                 }catch(Exception e){
29                         pln("Socket ��������" + e);
30                 }finally{
31                         closeAll();
32                 }
33         }
34     private void read(){
35                 String msg = "";
36                 try{
37                         while(true){
38                                 msg = dis.readUTF();
39                                 pln("���濡����client Msg : " + msg);
40                         }
41                 }catch(IOException ie){
42             pln("硫��吏��쎈� ��� : " +ie);
43                 }
44         }
45
46     BufferedReader br
47                 = new BufferedReader(new InputStreamReader(System.in));//Data Source
48         public void run(){ // �곕���� ��� ��
49             String str = null;
50             try{
51                         while((str = br.readLine()) != null){
52                                 dos.writeUTF(str); //dos : Data Destination
53                                 dos.flush();
54                         }
55                 }catch(IOException ie){
56                         pln("run() : " + ie.toString());
57                 }
58         }
59         private void closeAll(){
60                 try{
61                         if(dis != null) dis.close();
62                         if(dos != null) dos.close();
63                         if(is != null) is.close();
64                         if(os != null) os.close();
65                         if(s != null) s.close();
66                         if(ss != null) ss.close();
67                 }catch(IOException ie){}
68         }
69         private void pln(String str){
70                 System.out.println(str);
71         }
72         public static void main(String[] args)
73         {
74                 new SocketServer();
75         }
76 }
참고: 소스 브라우저를 사용하면서 도움이 필요하다면, TracBrowser를 참고하십시오.