一、单播
package com.example.tomcat.http;
import lombok.extern.slf4j.Slf4j;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
@Slf4j
public class SocketClientTest {
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("localhost", 8888);
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
outputStream.writeUTF("我是客户端,我想要获取XXX");
log.info("input=>{}", inputStream.readUTF());
socket.close();
} catch (IOException e) {
log.info("e=>{}", e.getMessage());
}
}
}
package com.example.tomcat.http;
import lombok.extern.slf4j.Slf4j;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
@Slf4j
public class SocketServerTest {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
log.info("服务器收到客户端的连接请求=>{} ", inputStream.readUTF());
outputStream.writeUTF("接收连接请求,连接成功");
socket.close();
serverSocket.close();
} catch (IOException e) {
log.error("e=>{}", e.getMessage());
}
}
}
二、组播
package com.example.tomcat.http;
import lombok.extern.slf4j.Slf4j;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
@Slf4j
public class Node1 {
private static int port = 8000;
private static String address = "228.0.0.4";
public static void main(String[] args) {
try {
InetAddress group = InetAddress.getByName(address);
MulticastSocket multicastSocket = new MulticastSocket(port);
multicastSocket.joinGroup(group);
while (true) {
String msg = "hello ! I am node1";
byte[] buffer = msg.getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port);
multicastSocket.send(packet);
Thread.sleep(1000);
}
} catch (Exception e) {
log.error("e=>{}", e.getMessage());
}
}
}
package com.example.tomcat.http;
import lombok.extern.slf4j.Slf4j;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
@Slf4j
public class Node2 {
private static int port = 8000;
private static String address = "228.0.0.4";
public static void main(String[] args) {
try {
InetAddress group = InetAddress.getByName(address);
MulticastSocket multicastSocket = new MulticastSocket(port);
multicastSocket.joinGroup(group);
byte[] buffer = new byte[1024];
while (true) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port);
multicastSocket.receive(packet);
String msg = new String(packet.getData(), 0, packet.getLength());
log.info("receive from=>{}", msg);
}
} catch (Exception e) {
log.error("e=>{}", e.getMessage());
}
}
}
三、广播
package com.example.tomcat.http;
import lombok.extern.slf4j.Slf4j;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
@Slf4j
public class BroadCastReceiver {
public static void main(String[] args) {
try{
DatagramSocket datagramSocket = new DatagramSocket(8888);
byte[] buf = new byte[5];
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
datagramSocket.receive(datagramPacket);
log.info("=====>msg {}", new String(buf));
} catch (Exception e) {
log.error("e=>{}", e.getMessage());
}
}
}
package com.example.tomcat.http;
import lombok.extern.slf4j.Slf4j;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
@Slf4j
public class BroadCastSender {
public static void main(String[] args) {
try {
InetAddress ip = InetAddress.getByName("192.168.0.255");
DatagramSocket datagramSocket = new DatagramSocket();
String msg = "hello !!!";
DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(), msg.getBytes().length, ip, 8888);
datagramSocket.send(datagramPacket);
datagramSocket.close();
} catch (Exception e) {
log.error("e=>{}", e.getMessage());
}
}
}