1 🚩TCP协议,三次握手,安全但是比较慢,适合传送大量数据
1.1 🏔️TCP编程用到的类
(1)InetAddress类
方法 |
说明 |
getLocalHost |
获取本机IneAddress对象 |
getByName |
根据制定主机名/域名获取IP地址 |
getHostName |
获取IneAddress对象的主机名 |
getHostAddress |
获取InetAddress对象地址 |
(2)Socket(插座)
使用步骤
服务端:
①创建 ServerSocket对象,指定端口
②ServersSocket的accept方法会返回一个Socket对象;【在服务器端获得插座】
③Socket对象提供了getOutputStream()方法和getInputStream()方法会返回字符流
客户端:
①创建Socket对象,指定服务器ip地址和端口
②通过Socket提供的方法,获得IO流进行相关操作
【注意: 发送完一次数据需要用,Socket提供的shutdownOutput()方法告诉对象已经发送完毕,最后要关闭Socket和IO流】
🌰server端代码示例:
public class serverDemo { public static void main(String[] args) throws IOException { // 创建ServerSocket对象 ServerSocket serverSocket = new ServerSocket(9999); System.out.println("服务器端口9999等待连接。。。"); // 获取Socket对象 Socket socket = serverSocket.accept(); InputStream inputStream = socket.getInputStream(); byte [] buffer = new byte[1024]; int leng = 0; while ((leng = inputStream.read(buffer)) != -1){ System.out.print(new String(buffer,0,leng)); } System.out.println(); // 回应数据 OutputStream outputStream = socket.getOutputStream(); byte [] bufferser = "你好client".getBytes(); outputStream.write(bufferser); socket.shutdownOutput(); System.out.println("server回应完毕"); socket.close(); inputStream.close(); outputStream.close(); } }
🌰client端代码示例:
public class ClientDemo { public static void main(String[] args) throws IOException { // 创建Socket对象,连接服务器 Socket socket = new Socket("127.0.0.1",9999); OutputStream outputStream = socket.getOutputStream(); byte [] buffer = "你好server".getBytes(); // 发送数据 outputStream.write(buffer); // 告诉对方发送完毕,否则会阻塞 socket.shutdownOutput(); System.out.println("client 第一次发送完毕"); // 获取回应 InputStream inputStream = socket.getInputStream(); byte [] bufferclient = new byte[1024]; int leng = 0; while ((leng = inputStream.read(bufferclient)) != -1){ System.out.print(new String(bufferclient,0,leng)); } System.out.println(); socket.close(); inputStream.close(); outputStream.close(); System.out.println("收到!退出"); } }
2 🚩UDP协议,可能数据丢失,适合小数据
🏔️DatagramSocket类和DatagramPacket类实现了UDP协议;
UDP编程没有明确的服务端和客户端;
🌰send端代码示例
public class SendDemo { public static void main(String[] args) throws IOException { // 创建DatagramSocket对象,用于接收发送,端口9988用于接收 DatagramSocket datagramSocket = new DatagramSocket(9988); byte [] sendBuffer = new byte[1024]; sendBuffer = "四大名著是啥1?".getBytes(); // 打包数据 DatagramPacket sendpacket = new DatagramPacket(sendBuffer, sendBuffer.length, InetAddress.getByName("127.0.0.1"), 9999); datagramSocket.send(sendpacket); System.out.println("提问完毕!"); // 接收回答 byte [] daan = new byte[1024]; DatagramPacket danpacket = new DatagramPacket(daan, daan.length); datagramSocket.receive(danpacket); // 拆包 int daleng = danpacket.getLength(); byte [] danfu = danpacket.getData(); System.out.println("回答:"+new String(danfu,0,daleng)); datagramSocket.close(); System.out.println("send端对话结束"); } }
🌰receive端代码示例:
public class ReceiveDemo { public static void main(String[] args) throws IOException { // 创建DatagramSocket对象,用于发送接受,9999端口等待接收 DatagramSocket datagramSocket = new DatagramSocket(9999); // 接收数据 byte [] reciveBuffer = new byte[1024]; DatagramPacket recdatagramPacket = new DatagramPacket(reciveBuffer,reciveBuffer.length); datagramSocket.receive(recdatagramPacket); // 拆包 int leng = recdatagramPacket.getLength(); byte huixin [] = recdatagramPacket.getData(); String str = new String(huixin,0,leng); if ("四大名著是啥?".equals(str)){ byte h1 [] = "三国、水浒、红楼、西游".getBytes(); DatagramPacket packet = new DatagramPacket(h1, h1.length, InetAddress.getByName("127.0.0.1"), 9988); datagramSocket.send(packet); }else { byte h1 [] = "what?".getBytes(); DatagramPacket packet = new DatagramPacket(h1, h1.length, InetAddress.getByName("127.0.0.1"), 9988); datagramSocket.send(packet); } datagramSocket.close(); System.out.println("receive端结束对话!"); } }