一. Socket 编程
Socket套接字,是由系统提供用于网络通信的技术,是基于TCP/IP协议的网络通信的基本操作单元。基于Socket套接字的网络程序开发就是网络编程;
java.net 包中提供了两种常见的网络协议的支持:
- TCP:TCP(英语:Transmission Control Protocol,传输控制协议) 是一种面向连接的、可靠的、基于字节流的传输层通信协议,TCP 层是位于 IP 层之上,应用层之下的中间层。TCP 保障了两个应用程序之间的可靠通信。通常用于互联网协议,被称 TCP / IP。
- UDP:UDP (英语:User Datagram Protocol,用户数据报协议),位于 OSI 模型的传输层,一个无连接的协议,提供了应用程序之间要发送数据的数据报。由于UDP缺乏可靠性且属于无连接协议,所以应用程序通常必须容许一些丢失、错误或重复的数据包。
二. 基于Udp的回显程序
1.服务器端
publicclassUdpServer { // 定义一个 soctet 对象privateDatagramSocketsocket=null; publicUdpServer(intport) throwsSocketException { //构造 socket ,要绑定相关的端口号socket=newDatagramSocket(port); } //启动服务器publicvoidstart() throwsIOException { System.out.println("服务器启动!"); while (true) { // 1.读取请求并解析// 构造DatagramPacket包,用来接收DatagramPacketrequestPacket=newDatagramPacket(newbyte[10000], 10000); socket.receive(requestPacket); //方便处理这个请求,把数据包转成String(这个操作不是非必须得)Stringrequest=newString(requestPacket.getData(), 0, requestPacket.getLength()); Stringresponse=process(request); // 2.把响应结果写到客户端DatagramPacketresponsePacket=newDatagramPacket(response.getBytes(), response.getBytes().length, requestPacket.getSocketAddress()); socket.send(responsePacket); System.out.printf("[%s:%d] req: %s,resp: %s\n", requestPacket.getAddress().toString(), requestPacket.getPort(), request, response); } } publicStringprocess(Stringrequest) { returnrequest; } publicstaticvoidmain(String[] args) throwsIOException { UdpServerudpServer=newUdpServer(9090); udpServer.start(); } }
2. 客户端
publicclassUdpClient { privateDatagramSocketsocket=null; privateStringserverIP; privateintserverPort; publicUdpClient(StringserverIP, intserverPort) throwsSocketException { // 客户端不需要显示关联端口,系统自动分配空闲端口socket=newDatagramSocket(); this.serverIP=serverIP; this.serverPort=serverPort; } Scannerscanner=newScanner(System.in); // 客户端和服务器进行多次交互publicvoidstart() throwsIOException { while (true) { // 1.先从控制读取字符串System.out.println("请输入: "); Stringrequest=scanner.next(); // 2.把字符串构造DatagramPacket包,用来存储要发送的信息DatagramPacketdatagramPacket=newDatagramPacket(request.getBytes(), request.getBytes().length, InetAddress.getByName(serverIP), serverPort); socket.send(datagramPacket); // 3.客户端读取服务器返回的响应DatagramPacketresponsePacket=newDatagramPacket(newbyte[10000], 10000); socket.receive(responsePacket); // 4.把响应数据转化成String显示出来Stringresponse=newString(responsePacket.getData(), 0, responsePacket.getLength()); System.out.printf("req: %s, resp: %s\n", request, response); } } publicstaticvoidmain(String[] args) throwsIOException { UdpClientudpClient=newUdpClient("127.0.0.1", 9090); udpClient.start(); } }
步骤:
1.服务器先启动, 等待客户端发送数据,执行到receive,此时服务端进入一个阻塞状态;
2.客户端开始输入操作,并进行send;
3.客户端send之后,走到receive读取响应,会阻塞等待;同时服务器这边就从receive返回,走到process生成响应,再走到send;
4. 客户端收到服务器端send之后发来的数据后,会解除阻塞;服务器进入下一轮循环,走到receive,等待下一个请求;
三. 基于Tcp的回显程序
1. 服务器端
publicclassTcpServer { privateServerSocketserverSocket=null; publicTcpServer(intport) throwsIOException { serverSocket=newServerSocket(port); } publicvoidstart() throwsIOException { ExecutorServiceexecutorService=Executors.newCachedThreadPool(); System.out.println("服务器启动!"); while (true) { SocketclientSocket=serverSocket.accept(); //写法1// 每次来一个新的客户端,都创建一个心得线程/*Thread thread = new Thread(()->{try {processConnect(clientSocket);} catch (IOException e) {e.printStackTrace();}});thread.start();*///写法2,使用线程池executorService.submit(newRunnable() { publicvoidrun() { try { processConnect(clientSocket); } catch (IOExceptione) { e.printStackTrace(); } } }); } } privatevoidprocessConnect(SocketclientSocket) throwsIOException { System.out.printf("[%s:%d] req: 客户端上线\n",clientSocket.getInetAddress().toString(),clientSocket.getPort()); try (InputStreaminputStream=clientSocket.getInputStream(); OutputStreamoutputStream=clientSocket.getOutputStream()) { Scannerscanner=newScanner(inputStream); PrintWriterprintWriter=newPrintWriter(outputStream); while (true) { // 1.读取请求if (!scanner.hasNext()) { // 读取的流到了结尾了System.out.printf("[%s:%d] 客户端下线!", clientSocket.getInetAddress().toString(),clientSocket.getPort()); break; } Stringrequest=scanner.next(); // 2.根据请求计算响应Stringresponse=process(request); // 3.把响应写回到客户端printWriter.println(response); System.out.printf("[%s:%d] req: %s;resp: %s\n",clientSocket.getInetAddress().toString(),clientSocket.getPort() ,request,response); } } catch (IOExceptione) { e.printStackTrace(); }finally { clientSocket.close(); } } privateStringprocess(Stringrequest) { returnrequest; } publicstaticvoidmain(String[] args) throwsIOException { TcpServertcpServer=newTcpServer(9090); tcpServer.start(); } }
2. 客户端
publicclassTcpClient { privateSocketsocket=null; publicTcpClient(StringserverIP, intport) throwsIOException { //服务器和客户端建立连接socket=newSocket(serverIP, port); } publicvoidstart() { Scannerscanner=newScanner(System.in); try (OutputStreamoutputStream=socket.getOutputStream(); InputStreaminputStream=socket.getInputStream()) { PrintWriterprintWriter=newPrintWriter(outputStream); ScannerscannerFromSocket=newScanner(inputStream); while (true) { // 1.从键盘上读取用户输入的内容System.out.println("请输入:"); Stringrequest=scanner.next(); // 2.读取的内容构造成请求,发送给服务器//这里只是把数据写到内存的缓存区,等到缓存区满了,才会写到网卡printWriter.println(request); //手动刷新缓冲区printWriter.flush(); // 3.从服务器读取响应内容Stringresponse=scannerFromSocket.next(); // 4.把响应结果显示到控制台上System.out.printf("req: %s;resp:%s\n", request, response); } } catch (IOExceptione) { e.printStackTrace(); } } publicstaticvoidmain(String[] args) throwsIOException { TcpClienttcpClient=newTcpClient("127.0.0.1",9090); tcpClient.start(); } }
步骤:
1. 服务器先执行strat方法,执行到accept这里进入阻塞状态,等待客户端建立连接
2.客户端启动,和服务器建立连接,连接成功之后,服务器的accept就会返回;
3.服务器尝试从客户端读取请求,此时会进入阻塞状态
4.客户端从控制台读取用户输入,输入完之后,客户端开始发送请求出去,同时读取服务器响应,进入阻塞状态;
5.服务器收到客户端的请求之后,把响应写回到客户端;同时客户端收到服务器的响应,就可以处理数据了;
6.双方都进入下一次循环