什么是socket ?
socket 是连接运行在网络上的两个程序间的双向通讯端点
服务器将某一套接字绑定到一个特定的端口,并通过这一套接字等待和监听客户端的的连接请求
客户端通过这个端口与服务器进行连接
如果一切正常,服务器接受到了连接的请求,并获得一个新的绑定到不同端口的套接字
服务端与客户通过读写套接字进行通讯
下面的基于Java 传统的tcp/ip 的代码编写,实现了服务器与客户端的通信
服务端代码:
package com.java.baseknowledge.net; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; import java.util.Map; public class ServiceSockets { private static HashMap<String,Socket> map = new HashMap<String,Socket>(); public static void main(String[] args) throws Exception { ServerSocket serverSocket =new ServerSocket(9099); while(true) { //阻塞方法,一旦有客户端与fwq连接,将会获得绑定新的绑定到不同端口到套接字与客户端进行通讯 Socket accept = serverSocket.accept(); TheMap.map.put(accept.getPort()+"", accept); System.err.println(TheMap.map); OutputThread run1 =new OutputThread(accept); Thread th1 =new Thread(run1); th1.start(); } } } /** * 全局存储连接的socket对象,进行广播📢通讯 * @author iscys * */ class TheMap{ public static HashMap<String,Socket> map = new HashMap<String,Socket>(); } class OutputThread implements Runnable{ private Socket socket; public OutputThread(Socket socket) { this.socket=socket; } InputStream inputStream; @Override public void run() { System.out.println(TheMap.map); try { inputStream = socket.getInputStream(); while(true) { byte [] b =new byte[2048]; while(-1!=(inputStream.read(b))) { //进行广播 TheMap.map.forEach((k,v)->{ try{ if(v!=socket) { OutputStream outputStream = v.getOutputStream(); outputStream.write(b); } } catch(Exception e) { e.printStackTrace(); } }); } } } catch(Exception e) { e.printStackTrace(); } } }
客户端代码:
package com.java.baseknowledge.net; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.Map; public class ClientSocket { public static void main(String[] args) throws Exception, IOException { Socket socket =new Socket("localhost",9099); OutputThread2 run1 =new OutputThread2(socket); InputThread3 run2 =new InputThread3(socket); Thread th1 =new Thread(run1); Thread th2 =new Thread(run2); th1.start(); th2.start(); } } class OutputThread2 implements Runnable{ private Socket socket; public OutputThread2(Socket socket) { this.socket=socket; } @Override public void run() { try { OutputStream outputStream = socket.getOutputStream(); while(true) { BufferedReader bufferefReader = new BufferedReader(new InputStreamReader(System.in)); String readLine = bufferefReader.readLine(); outputStream.write(readLine.getBytes()); } //outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class InputThread3 implements Runnable{ private Socket socket; public InputThread3(Socket socket) { this.socket=socket; } @Override public void run() { while(true) try { InputStream inputStream = socket.getInputStream(); byte [] b =new byte[2048]; int length =inputStream.read(b); System.out.println(new String(b)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
原创打造,多多指教