客户端套接字无法从服务器套接字接收,除非服务器套接字端口是硬编码的

时间:2016-05-31 05:24:43

标签: java sockets udp port

  • 我使用默认构造函数创建一个客户端套接字。
  • 我通过端口45500向服务器发送数据包。
  • 我在服务器端通过端口45500
  • 收到数据包
  • 我在服务器端创建一个新套接字
  • 我用新的serverSocket发送一个ack。 ack被发送到receivedPacket.getport()
  • 客户端什么也没收到,除非我不在服务器端创建新的套接字,只是发送带有旧套接字的ack(带有硬编码端口45500)
  • 我不知道发生了什么事!

客户端

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)); 

1 个答案:

答案 0 :(得分:0)

双方停用windows10防火墙就行了!