I know that at the beginning of the handshake, the client should fill in the Destination IP (For IP Layer) and Destination Port numbers for the segment to be correctly delivered to the server. A client can learn the Destination IP by using the DNS service, but how does TCP know the Destination Port number (suppose it is not a well-known port), at the first step of the handshake?


For most applications the port is actually well-known. These are usually maintained by IANA. Then there are product specific protocols which are not maintained by IANA but where the (default) ports are coded into the product itself. Any ports outside these defaults are usually explicitly given by the user (like using a URL like http://host:3000/ for port 3000 instead of http://host/ for default port 80).

suppose it is not a well-known port ...

And then there are some mechanism where ports gets assigned dynamically or where ports are looked up using some external mechanism. For example FTP (file transfer protocol) uses TCP connection for data transfer which are separate from the control connection which only transfers commands. The ports for these data connections get dynamically assigned and then send inside the control connection using commands like PORT, PASV, EPRT, EPSV to the peer so that it knows which port to connect to. A similar mechanism is for exchange of RTP data (usually UDP not TCP) inside SIP (a VoIP protocol). Additionally some protocols (like SIP) might use DNS SRV records to look up the IP and port needed to connect to a specific service.

In other words: there is no single mechanism to find out the port. There are a few common mechanisms adapted by several protocols (i.e. IANA maintained, hardcoded into product, SRV records) but there are also product- or protocol specific mechanism (like in FTP). To find out which mechanism gets actually used look at the specific protocol.


how does TCP know the Destination Port number (suppose it is not a well-known port), at the first step of the handshake?

TCP is told which port to connect to by the application opening the socket. The application needs to know. Commonly, the application just uses a 'well-known port' - like TCP port 80 for HTTP. Otherwise the application has to be configured or instructed to use another port. Rarely, it just guesses and tries various ports.


First, understand that TCP, itself, knows nothing about clients or servers, which are an application concept. TCP creates connections between peers.

There are some standard port assignments for services. These are maintained in by IANA in the Service Name and Transport Protocol Port Number Registry.

For example, TCP port 80 is reserved for HTTP. If a web browser wants to communicate with an HTTP server, it will default to sending requests to TCP port 80. That is not to say that an HTTP server cannot use a different TCP port, but a web browser would need to know ahead of trying to connect that the HTTP server is using a non-standard TCP port.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.