01.package socket;  
02.  
03.import java.net.*;  
04.import java.io.*;  
05.  
06.public class ServerMain   
07.{  
08.    public static void main(String args[])   
09.    {  
10.        try   
11.        {  
12.            boolean flag = true;  
13.            Socket clientSocket = null;  
14.            String inputLine;  
15.            int c;  
16.  
17.            ServerSocket sSocket = new ServerSocket(8018);  
18.            System.out.println("Server listen on:" + sSocket.getLocalPort());  
19.            while(flag)   
20.            {  
21.                clientSocket = sSocket.accept();//接受客户端数据 
22.                  
23.         

24.                DataInputStream is = new DataInputStream(  
25.                        new BufferedInputStream(clientSocket.getInputStream()));  
26.                  
27.                OutputStream os = clientSocket.getOutputStream();//得到客户端的输出流

28.                  
29.                while ((inputLine = is.readLine()) != null)   
30.                {    
31.                    //当输入不为空-joh  
32.                    // 当客户端输入stop时,程序将自行终止

33.                    if (inputLine.equals("stop"))   
34.                    {  
35.                        flag = false;  
36.                        break;  
37.                    }   
38.                    else   
39.                    {  
40.                        System.out.println("Client Message is:"+inputLine);//显示输入  
41.                        while ((c = System.in.read()) != -1)   
42.                        {  
43.                            os.write((byte) c);  
44.                            if (c == '/n')   
45.                            {  
46.                                os.flush(); // 将信息发送到客户端

47.                                break;  
48.                            }  
49.                        }  
50.                    }  
51.                }  
52.                is.close();  
53.                os.close();  
54.                clientSocket.close();  
55.            }  
56.            sSocket.close();  
57.        }   
58.        catch (Exception e)   
59.        {  
60.            System.out.println("Exception :" + e.getMessage());  
61.        }  
62.    }  
63.}  

客户端程序:

 

[c-sharp] view plaincopy
01.package client;  
02.  
03.import java.net.*;  
04.import java.io.*;  
05.import java.lang.*;  
06.  
07.public class ClientMain   
08.{  
09.    public static void main(String args[])   
10.    {  
11.        try   
12.        {  
14.            Socket cSocket = new Socket("10.148.53.188", 8018);  
15.            //打开流

16.            OutputStream os = cSocket.getOutputStream();  
17.            DataInputStream is = new DataInputStream(cSocket.getInputStream());  
18.  
19.            int c;  
20.            boolean flag = true;  
21.  
22.            String responseline;  
23.  
24.            while (flag)   
25.            {  
26.                // 从标准输入输出接收字符  
27.                while ((c = System.in.read()) != -1)  
28.                {  
29.                    //把读入的数据写进去

30.                    os.write((byte) c);  
31.                    if (c == '/n')   
32.                    {  
33.                        os.flush();  
34.                        // 將程序阻塞,直到信息收到时才释放

35.                        responseline = is.readLine();  
36.                        System.out.println("Server Message is:" + responseline);  
37.                    }  
38.                }  
39.            }  
40.            os.close();  
41.            is.close();  
42.            cSocket.close();  
43.  
44.        }   
45.        catch (Exception e)   
46.        {  
47.            System.out.println("Exception :" + e.getMessage());  
48.        }  
49.    }  
50.}