基于多线程的TCP socket通信经典案例
Posted ThinkVenus
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于多线程的TCP socket通信经典案例相关的知识,希望对你有一定的参考价值。
服务器端
package com.thinkvenus.study.socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; /** * 服务器端线程处理类 */ public class ThreadServer extends Thread { private Socket socket; public ThreadServer(Socket socket) { this.socket = socket; } /** * 线程执行的操作,响应客户端请求 */ @Override public void run() { InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; OutputStream os = null; PrintWriter pw = null; try { is = socket.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String msg; while((msg = br.readLine()) != null) { System.out.println("这里是服务器端,客户端说:" + msg); } socket.shutdownInput();//关闭输入 //获取输出流,响应客户端的请求 os = socket.getOutputStream(); pw = new PrintWriter(os); pw.write("欢迎您"); pw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { //关闭资源 try { if(pw != null) pw.close(); if(os != null) os.close(); if(br != null) br.close(); if(isr != null) isr.close(); if(is != null) is.close(); if(socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
package com.thinkvenus.study.socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; /** * 基于TCP的socket通信 */ public class Server { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("*****************服务端即将启动,等待客户端的连接**********"); int count = 0; //循环监听客户端的连接 while(true) { Socket socket = serverSocket.accept();//开始监听 ThreadServer ts = new ThreadServer(socket); ts.start(); count++; System.out.println("客户端的数量:"+count); } } catch (IOException e) { e.printStackTrace(); } } }
客户端:
package com.thinkvenus.study.socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) { try { Socket socket = new Socket("localhost", 8888); OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流 pw.println("username=aaron; pwd="+Math.random()); pw.flush(); socket.shutdownOutput();//关闭输出 //获取输入流,获取服务器端的响应 InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String msg; while((msg = br.readLine()) != null) { System.out.println("我是客户端,服务端说:"+ msg); } //关闭资源 br.close(); isr.close(); is.close(); pw.close(); os.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
以上是关于基于多线程的TCP socket通信经典案例的主要内容,如果未能解决你的问题,请参考以下文章