socket通信原理
Java多线程实现Socket通讯
1、服务端
import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * <p> * 基于socket通讯-服务端 * <p> * * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a> * @version * @since 2017年1月12日 */ public class Server { private static ServerSocket SERVER_SOCKET =null;; static{ try { SERVER_SOCKET = new ServerSocket(9090); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { try { System.out.println("******服务器已启动,等待客户端连接*****"); Socket socket = null; while(true){ //循环监听客户端的连接 socket = SERVER_SOCKET.accept(); //新建一个线程ServerSocket,并开启 new ServerSocketThread(socket).start(); } } catch (IOException e) { e.printStackTrace(); } } }
2、服务端线程处理类
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; /** * <p> * Socket线程服务类 * <p> * * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a> * @version * @since 2017年1月13日 */ public class ServerSocketThread extends Thread { private Socket socket; public ServerSocketThread(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 { // socket获取字节输入流 is = socket.getInputStream(); // 将字节输入流转字符输入流 isr = new InputStreamReader(is); // 将字符输入流转行输入流 br = new BufferedReader(isr); // String message = null; while ((message = br.readLine()) != null) { System.out.println("客户端发来消息:" + message); } // 必须先关闭输入流才能获取下面的输出流 socket.shutdownInput(); // 获取输出流 os = socket.getOutputStream(); pw = new PrintWriter(os); pw.write("欢饮您进入socket"); pw.flush(); // 关闭输入流 socket.shutdownOutput(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭资源 if (pw != null) { pw.close(); } try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (isr != null) { isr.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (isr != null) { isr.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
3、客户端
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; /** * <p> * 基于socket通讯-客户端 * <p> * * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a> * @version * @since 2017年1月12日 */ public class Client { public static void main(String[] args) { Socket socket = null; OutputStream os = null; PrintWriter pw = null; InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; try { socket = new Socket("127.0.0.1", 9090); // 获取输出流向服务端写入数据 os = socket.getOutputStream(); pw = new PrintWriter(os); pw.write("用户名:admin 密码:123"); pw.flush(); socket.shutdownOutput(); // 获取输入流接受服务端返回的信息 is = socket.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String message = null; while ((message = br.readLine()) != null) { System.out.println("服务器说:" + message); } socket.shutdownInput(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (isr != null) { isr.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } if (pw != null) { pw.close(); } try { if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } }