简体   繁体   中英

client socket cannot receive from server socket unless server socket port is hardcoded

  • I create a client socket with default constructor.
  • I send a packet to the server on port 45500.
  • I receive the packet on server-side on port 45500
  • I create a new socket on server-side
  • I send an ack with the new serverSocket. The ack is sent to the receivedPacket.getport()
  • The client receives nothing UNLESS I don't create a new socket on server-side and just send the ack with the old socket (with hardcoded port 45500)
  • I have no clue what's going on!

client

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

server

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

停用两侧的Windows10防火墙就可以了!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM