简体   繁体   中英

Sending/receiving packets on networked computers with different UDP ports

If I have two or more computers on a LAN communicating via UDP, do they all have to be using the same port to be able to receive messages from one another? For example, say my desktop is using port 1550 to send a message and my notebook, which is also on the LAN, is using port 3746 to listen for and receive messages. Would sending using just the destination IP in such a situation work? If not, would each computer have to know and specify the receiver's port?

For a UDP or TCP endpoint an address and port must be set. The initiator must know what address-port to send to. The receiver can use the getpeername() call to determine the address-port combination of the sender and then use this information to build up the address structure for a reply message. The two do not need to use the same port number.

do they all have to be using the same port to be able to receive messages from one another

No. There is no requirement for servers and clients to use the same port numbers; indeed this is typically infeasible. But each sender has to know the port number of the desired receiver. In the case of a response, the source address and port came in with the request. In the case of a request, the client has to already know the server ip:port, somehow.

communicating in the application layer,you must specify the destination ip and port. if in the bottom layer(physical frame),you could just send the frame to the destination.

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