JAVA Socket编程实例代码

  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.io.PrintWriter;  
  5. import java.net.ServerSocket;  
  6. import java.net.Socket;  
  7.   
  8. public class Server   
  9. {  
  10.     private ServerSocket ss;  
  11.     private Socket socket;  
  12.     private BufferedReader in;  
  13.     private PrintWriter out;  
  14.   
  15.     public Server()   
  16.     {  
  17.         try   
  18.         {  
  19.             ss = new ServerSocket(10000);  
  20.               
  21.             System.out.println(“The server is waiting your input…”);  
  22.               
  23.             while(true)   
  24.             {  
  25.                 socket = ss.accept();  
  26.                   
  27.                 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
  28.                 out = new PrintWriter(socket.getOutputStream(), true);  
  29.                 String line = in.readLine();  
  30.                   
  31.                 System.out.println(“you input is : “ + line);  
  32.                   
  33.                 //out.println(“you input is :” + line);  
  34.                   
  35.                 out.close();  
  36.                 in.close();  
  37.                 socket.close();  
  38.                   
  39.                 if(line.equalsIgnoreCase(“quit”) || line.equalsIgnoreCase(“exit”))  
  40.                     break;  
  41.             }  
  42.               
  43.             ss.close();  
  44.               
  45.         } catch (IOException e) {  
  46.             e.printStackTrace();  
  47.         }  
  48.     }  
  49.   
  50.     public static void main(String[] args)   
  51.     {  
  52.         new Server();  
  53.     }  
  54. }  
 
客户端 java的
  1. import java.io.*;  
  2. import java.net.*;  
  3.   
  4. public class Client   
  5. {  
  6.     Socket socket;  
  7.     BufferedReader in;  
  8.     PrintWriter out;  
  9.   
  10.     public Client()   
  11.     {  
  12.         try   
  13.         {  
  14.             socket = new Socket(“127.0.0.1”10000);  
  15.               
  16.             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
  17.             out = new PrintWriter(socket.getOutputStream(), true);  
  18.             BufferedReader line = new BufferedReader(new InputStreamReader(System.in));  
  19.   
  20.             out.println(line.readLine());  
  21.               
  22.             line.close();  
  23.             out.close();  
  24.             in.close();  
  25.               
  26.             socket.close();  
  27.               
  28.         } catch (IOException e) {  
  29.         }  
  30.     }  
  31.   
  32.     public static void main(String[] args)   
  33.     {  
  34.         new Client();  
  35.     }  
  36. }  
 
转载自:

版权声明:本文为wuhenke原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wuhenke/archive/2011/12/13/2286634.html