Tcp – port numbers for TCP and UDP

layer4protocol-theorytcptransport-protocoludp

Why do we need both source and destination port numbers in transport layer protocols such as TCP or UDP? What is the purpose for having both?

Best Answer

Just as MAC addresses are layer-2 addresses, and IP addresses are layer-3 addresses, port numbers are layer-4 addresses. When the transport layer wants to reply to a layer-4 request, it must have the address (port) to which it should reply.

When a PC sends an HTTP (TCP port 80) request to to an HTTP server, the PC uses an ephemeral TCP source port and the HTTP server's TCP port 80 as the destination port. The HTTP server replies back to the PC's ephemeral port, and it gets that port number from the source port of the PC's request.