Avik
Avik

Reputation: 2137

Port Number

This is a small doubt and something that shouldn't really come to one's mind.So please forgive me for this

In order to transmit messages between two host on lan, do the port numbers to send and receive data have to be the same?

Upvotes: 1

Views: 433

Answers (4)

Robert Deml
Robert Deml

Reputation: 12532

Just to add my $0.02, the server can have multiple connections open on the same socket. So if you have 3 clients all connecting to port 80, they can all be connected at the same time; you don't have to worry about one client "hogging" the port.

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272217

No. You will send to a known port number, but your client-side port number will essentially be random. This means that multiple clients can send to one server, using different client-side port numbers, but only the one known server-side number.

e.g. if you have multiple clients on one machine talking to a remote webserver, it would look like:

localhost:31000 -> webserver:80
localhost:31001 -> webserver:80
localhost:31002 -> webserver:80

and you'd only have to specify the webserver:80 combination. The client side numbers are ephemeral (see here for more info)

Upvotes: 2

blowdart
blowdart

Reputation: 56490

No.

It works as follows

  1. Client machine wants to talk to server machine Client machine needs to know the port on the server machine, for example port 80 for http
  2. Client machine opens a connection to server machine. It is opened on a random port on the client, but to the known port number on the server
  3. The server sends back along this pipe to the port number the client tells it to, the random one it opened

Upvotes: 1

pgb
pgb

Reputation: 25001

No, they don't. You need a well known port to establish the connection on the host, but the client will create a socket with a random port number returned by the OS.

Upvotes: 0

Related Questions