Transport protocols carry port information in their protocol headers. The TCP and UDP headers both carry a pair of ports:
A computer may send UDP packets without first establishing a connection to the recipient. To transmit a transport packet, a computer completes the appropriate fields in the transport header and forwards the data together with the header for transmission by the IP network layer.
Port Fields in the UDP protocol header
Generally, clients set the source port number to a unique number that they choose themselves - usually based on the program that started the connection. Since this number is returned by the server in responses, this lets the sender know which "conversation" incoming packets are to be sent to.
Some ports (low numbered) are well-known, and are each associated with a particular service that uses the transport layer. The destination port of packets sent by the client is usually set to one of a number of well-known ports. These usually correspond to one of a number of different applications, e.g. port 23 is used for telnet, and port 80 is used for web servers.
20 FTP-DATA File Transfer [Default Data] 21 FTP File Transfer [Control] 23 TELNET Telnet 25 SMTP Simple Mail Transfer 37 TIME Time 69 TFTP Trivial File Transfer 79 FINGER Finger 110 POP3 Post Office Protocol v 3 123 NTP Network Time Protocol 143 IMAP2 Interim Mail Access Prot. v2 161 SNMP Simple Network Man. Prot.
Some well-known UDP/IP port numbers
(a full list is provided in the link at the bottom of this page).
A server process (program), listens for packets received with a particular well-known port number and tells its local UDP layer to send packets matching this destination port number to the server program. It determines which client these packets come from by examining the received IP source address and the received unique UDP source port number. Any responses which the server needs to send to back to a client are sent with the source port number of the server (the well-known port number) and the destination port selected by the client. Most people do not memorise the well known ports, instead they look them up in table (e.g. see below).
Clients and Servers using ports to communicate at the transport layer
If a client/server application executes on a host with more than one IP interface, the application needs to ensure that it sends any UDP responses with an IP source address that matches the IP destination address of the packet that carried the request.
Example Packet Decodes
J. Postel. User Datagram Protocol, RFC 768.
R.T. Braden, D.A. Borman, C. Partridge, Computing the Internet Checksum, RFC 1071
L. Eggert, G. Fairhurst, Unicast UDP Usage Guidelines for Application Designers, RFC4505
List of Assigned UDP Port Numbers