Java Socket TCP编程(Server端多线程处理)
1 package com; 2 3 import java.io.*; 4 import java.net.Socket; 5 6 /** 7 * Socket Client 8 * <p> 9 * Created by Administrator on 2018/5/2. 10 */ 11 public class Client { 12 13 public static void main(String[] args) { 14 15 try { 16 17 // 1.初始化客户端socket连接 18 Socket socket = new Socket("localhost", 8888); 19 20 // 2.client发送消息 21 OutputStream outputStream = socket.getOutputStream(); 22 PrintWriter printWriter = new PrintWriter(outputStream); 23 printWriter.write("[name:jim, pwd:123]"); 24 printWriter.flush(); 25 socket.shutdownOutput(); 26 27 // 3.client接收消息 28 InputStream inputStream = socket.getInputStream(); 29 InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 30 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 31 String str; 32 while ((str = bufferedReader.readLine()) != null) { 33 System.out.println("I am Client, now get message from Server:" + str); 34 } 35 36 // 4.关闭资源 37 bufferedReader.close(); 38 inputStreamReader.close(); 39 inputStream.close(); 40 41 printWriter.close(); 42 outputStream.close(); 43 socket.close(); 44 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } 48 } 49 }
1 package com; 2 3 import java.io.*; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 7 /** 8 * Socket Server 9 * <p> 10 * Created by Administrator on 2018/5/2. 11 */ 12 public class Server { 13 14 public static void main(String[] args) { 15 16 try { 17 18 // 初始化服务端socket连接,并监听8888端口的socket请求 19 ServerSocket serverSocket = new ServerSocket(8888); 20 21 System.out.println("****** I am Server, now begin to wait the client ******"); 22 23 int count = 0; 24 25 // 处理socket请求 26 Socket socket = null; 27 while (true) { 28 29 socket = serverSocket.accept(); 30 ServerThread serverThread = new ServerThread(socket); 31 System.out.println("client host address is: " + socket.getInetAddress().getHostAddress()); 32 serverThread.start(); 33 count++; 34 System.out.println("now client count is: " + count); 35 36 } 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 }
1 package com; 2 3 import java.io.*; 4 import java.net.Socket; 5 6 /** 7 * Created by Administrator on 2018/5/3. 8 */ 9 public class ServerThread extends Thread { 10 11 private Socket socket; 12 13 public ServerThread(Socket socket) { 14 this.socket = socket; 15 } 16 17 @Override 18 public void run() { 19 20 InputStream inputStream = null; 21 InputStreamReader inputStreamReader = null; 22 BufferedReader bufferedReader = null; 23 OutputStream outputStream = null; 24 PrintWriter printWriter = null; 25 26 try { 27 28 // server接收消息 29 inputStream = socket.getInputStream(); 30 inputStreamReader = new InputStreamReader(inputStream); 31 bufferedReader = new BufferedReader(inputStreamReader); 32 33 String str; 34 if ((str = bufferedReader.readLine()) != null) { 35 System.out.println("I am Server, now get message from Client: " + str); 36 } 37 socket.shutdownInput(); 38 39 // server发送消息 40 outputStream = socket.getOutputStream(); 41 printWriter = new PrintWriter(outputStream); 42 printWriter.write("[welcome to you!]"); 43 printWriter.flush(); 44 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } finally { 48 // 关闭资源 49 try { 50 if (printWriter != null) { 51 printWriter.close(); 52 } 53 if (outputStream != null) { 54 outputStream.close(); 55 56 } 57 if (bufferedReader != null) { 58 bufferedReader.close(); 59 } 60 if (inputStreamReader != null) { 61 inputStreamReader.close(); 62 } 63 if (inputStream != null) { 64 inputStream.close(); 65 } 66 if (socket != null) { 67 socket.close(); 68 } 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } 72 } 73 } 74 }