Gemini14
Gemini14

Reputation: 6254

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?

Upvotes: 1

Views: 1054

Answers (3)

user207421
user207421

Reputation: 310850

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.

Upvotes: 2

shenju
shenju

Reputation: 29

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.

Upvotes: 1

ribram
ribram

Reputation: 2450

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.

Upvotes: 4

Related Questions