Java Socket多线程服务端、客户端

时间:2022-05-05 22:11:39

Java Socket多线程服务端、客户端

分类: JAVA 87人阅读 评论(0) 收藏 举报
  1. //主方法  
  2. package com.huhai.demo.socket;  
  3.   
  4. public class Main {  
  5.   
  6.     /** 功能说明: 
  7.      * @param args 
  8.      * @author 胡海 
  9.      * @throws InterruptedException  
  10.      * @since 2011-11-8 下午05:08:05 
  11.      */  
  12.   
  13.     public static void main(String[] args) throws InterruptedException {  
  14.         Thread serverTthread = new Thread(new Runnable(){  
  15.                 public void run(){  
  16.                     new SocketServer();  
  17.                 }  
  18.         });  
  19.         serverTthread.start();  
  20.         Thread.sleep(1000);  
  21.         Thread clientThread = new Thread(new Runnable(){  
  22.                 public void run(){  
  23.                     new SocketClient();  
  24.                 }  
  25.         });  
  26.         clientThread.start();  
  27.     }  
  28.   
  29. }  
  30.   
  31.   
  32.   
  33. //客户端  
  34. <pre name="code" class="java">package com.huhai.demo.socket;  
  35.   
  36. import java.io.BufferedReader;  
  37. import java.io.InputStreamReader;  
  38. import java.io.PrintWriter;  
  39. import java.net.Socket;  
  40.   
  41. public class SocketClient {  
  42.     private Socket socket;  
  43.     private int port = 10000;  
  44.     private String hostIp = "127.0.0.1";  
  45.       
  46.     public SocketClient(){  
  47.         try {  
  48.             String message = null;  
  49.             BufferedReader in = null;  
  50.             PrintWriter out = null;  
  51.             BufferedReader line = null;  
  52.             while(!SocketCommand.CONST_QUIT.equals(message)){  
  53.                 socket = new Socket(hostIp, port);  
  54.                 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
  55.                 out = new PrintWriter(socket.getOutputStream(), true);  
  56.                 line = new BufferedReader(new InputStreamReader(System.in));  
  57.                   
  58.                 System.out.println("please input some characters :");  
  59.                 message = line.readLine();  
  60.                 out.println(message);  
  61.                 System.out.println("Back message from server:[" + in.readLine() + "]");  
  62.             }  
  63.             System.out.println("Client exit.");  
  64.             line.close();  
  65.             out.close();  
  66.             in.close();  
  67.             socket.close();  
  68.         } catch (Exception e) {  
  69.             e.printStackTrace();  
  70.         }  
  71.     }  
  72.   
  73.     public static void main(String[] args) {  
  74.         new SocketClient();  
  75.   
  76.     }  
  77.   
  78. }  

//服务端
  1. package com.huhai.demo.socket;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.io.PrintWriter;  
  7. import java.net.ServerSocket;  
  8. import java.net.Socket;  
  9. import java.util.concurrent.ExecutorService;  
  10. import java.util.concurrent.Executors;  
  11.   
  12. public class SocketServer {  
  13.     private ServerSocket serverSocket;  
  14.     private Socket socket;  
  15.     private int port = 10000;  
  16.     private ExecutorService executeService = Executors.newFixedThreadPool(10);  
  17.       
  18.     public SocketServer(){  
  19.         try {  
  20.             serverSocket = new ServerSocket(port);  
  21. //          serverSocket = new ServerSocket(port, 10, InetAddress.getByName("192.168.1.102"));  
  22.             System.out.println("SocketServer have been started.[" + serverSocket + "]");  
  23.             while(true){  
  24.                     socket = serverSocket.accept();  
  25.                     executeService.submit(new Runnable(){  
  26.                         public void run(){  
  27.                             try{  
  28.                                 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
  29.                                 PrintWriter out = new PrintWriter(socket.getOutputStream(), true);  
  30.                                   
  31.                                 String line =  in.readLine();  
  32.                                 System.out.println("Server Received:[" + line + "]");  
  33.                                 out.println("Server Received:[" + line + "]");  
  34.                                 out.close();  
  35.                                 in.close();  
  36.                             }catch (Exception e) {  
  37.                                 e.printStackTrace();  
  38.                             }finally{  
  39.                                 try {  
  40.                                     socket.close();  
  41.                                 } catch (IOException e) {  
  42.                                     System.out.println("close socket error.");  
  43.                                     e.printStackTrace();  
  44.                                 }  
  45.                             }  
  46.                         }  
  47.                     });  
  48.             }  
  49.         } catch (Exception e) {  
  50.             e.printStackTrace();  
  51.         }finally{  
  52.             try {  
  53.                 if(null != serverSocket){  
  54.                     serverSocket.close();  
  55.                     System.out.println("serverSocket close");  
  56.                 }  
  57.             } catch (Exception e) {  
  58.                 e.printStackTrace();  
  59.             }  
  60.         }  
  61.     }  
  62.       
  63.     public static void main(String[] args){  
  64.         new SocketServer();  
  65.     }  
  66.       
  67. }  

//常量
  1. package com.huhai.demo.socket;  
  2.   
  3. public class SocketCommand {  
  4.     public static final String CONST_QUIT = "quit";  
  5. }  

运行结果:Java Socket多线程服务端、客户端

上一篇: JFace Dialog 调整大小