TCP
客户端发送消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| try { Socket socket = new Socket("localhost", 7171);
OutputStream outputStream = socket.getOutputStream(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
Scanner scanner = new Scanner(System.in);
while (true) { String text = scanner.nextLine(); bufferedWriter.write(text); bufferedWriter.newLine(); bufferedWriter.flush();
if ("exit".equals(text)) break; }
socket.close(); } catch (IOException e) { throw new RuntimeException(e); }
|
服务端接收消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| try { ServerSocket serverSocket = new ServerSocket(7171); System.out.println("等待客户端连接"); Socket socket = serverSocket.accept(); System.out.println("客户端连接成功");
InputStream inputStream = socket.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
while (true) { String text = bufferedReader.readLine(); System.out.println("收到客户端消息: " + text);
if ("exit".equals(text)) break; }
serverSocket.close(); } catch (IOException e) { throw new RuntimeException(e); }
|
UDP
单播
发送消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| try { int port = 3000;
String text = "hello"; byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
InetAddress inetAddress = InetAddress.getByName("192.168.110.116"); DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, inetAddress, port);
DatagramSocket datagramSocket = new DatagramSocket(); datagramSocket.send(datagramPacket);
datagramSocket.close(); } catch (IOException e) { throw new RuntimeException(e); }
|
接收消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| try { int port = 3000;
byte[] buffer = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
DatagramSocket datagramSocket = new DatagramSocket(port); datagramSocket.receive(datagramPacket);
byte[] data = datagramPacket.getData(); int length = datagramPacket.getLength(); int offset = datagramPacket.getOffset(); InetAddress address = datagramPacket.getAddress(); int packetPort = datagramPacket.getPort();
String message = new String(data, offset, length);
datagramSocket.close();
System.out.println("message = " + message + " - address = " + address); } catch (IOException e) { throw new RuntimeException(e); }
|
组播
组播要求发送和所有接收的主机在同一网段,因此只适用于局域网内通信。组播地址是D
类IP
地址的一个子集,专门用于支持一对多的网络通信。D
类IP
地址的范围是从224.0.0.0
到239.255.255.255
发送组播消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| try { int port = 3000;
String text = "hello"; byte[] data = text.getBytes(StandardCharsets.UTF_8);
InetAddress inetAddress = InetAddress.getByName("224.0.0.1");
DatagramPacket datagramPacket = new DatagramPacket(data, data.length, inetAddress, port);
MulticastSocket multicastSocket = new MulticastSocket();
multicastSocket.send(datagramPacket);
multicastSocket.close(); } catch (IOException e) { throw new RuntimeException(e); }
|
接收组播消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| try { int port = 3000;
byte[] buffer = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
InetAddress inetAddress = InetAddress.getByName("224.0.0.1");
MulticastSocket multicastSocket = new MulticastSocket(port);
multicastSocket.joinGroup(inetAddress);
multicastSocket.receive(datagramPacket);
byte[] packetData = datagramPacket.getData(); int length = datagramPacket.getLength(); int offset = datagramPacket.getOffset(); InetAddress address = datagramPacket.getAddress(); int packetPort = datagramPacket.getPort();
String message = new String(packetData, offset, length);
System.out.println("Received: " + message);
multicastSocket.close(); } catch (IOException e) { throw new RuntimeException(e); }
|
广播
发送广播消息需要设置广播权限,其实代码和单播差不太多。广播要求发送和所有接收的主机在同一网段,典型地址为255.255.255.255
发送广播消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| try { int post = 3000;
String text = "hello"; byte[] data = text.getBytes(StandardCharsets.UTF_8);
InetAddress inetAddress = InetAddress.getByName("255.255.255.255"); DatagramPacket datagramPacket = new DatagramPacket(data, data.length, inetAddress, post);
DatagramSocket datagramSocket = new DatagramSocket(); datagramSocket.setBroadcast(true);
datagramSocket.send(datagramPacket);
datagramSocket.close(); } catch (IOException e) { throw new RuntimeException(e); }
|
接收广播消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| try { int port = 3000;
byte[] buffer = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
DatagramSocket datagramSocket = new DatagramSocket(port); datagramSocket.receive(datagramPacket);
byte[] data = datagramPacket.getData(); int length = datagramPacket.getLength(); int offset = datagramPacket.getOffset(); InetAddress address = datagramPacket.getAddress(); int packetPort = datagramPacket.getPort();
String message = new String(data, offset, length);
datagramSocket.close();
System.out.println("message = " + message + " - address = " + address); } catch (IOException e) { throw new RuntimeException(e); }
|