客户端
DatagramSocket clientSocket = new DatagramSocket();
byte[] receiveData = new byte[5000];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
byte[] connection = connectionRequest.getBytes();
InetAddress IPAdress = InetAddress.getByName("169.xxx.xx.xxx");
DatagramPacket packet = new DatagramPacket(connection, connection.length, IPAdress, 45500);
clientSocket.send(packet);
// doesn't receive unless port #45500 on server side sends!
clientSocket.receive(receivePacket);
服务器
DatagramSocket connectionSocket = new DatagramSocket(45500);
byte[] receiveData = new byte[5000];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
connectionSocket.receive(receivePacket);
int clientPort = receivePacket.getPort();
InetAddress IP = receivePacket.getAddress();
byte[] sendData = ("accepted").getBytes();
DatagramSocket serverSocket = new DatagramSocket();
// doesn't work unless connectionSocket (port 45500) sends!
serverSocket.send(new DatagramPacket(sendData, sendData.length, IP, clientPort));
答案 0 :(得分:0)
双方停用windows10防火墙就行了!