|
|
A calling program issues a socket call to AF_INET (Address Family Internet) to get access to the net. TCP returns a file descriptor. If the calling program wishes a particular circuit, it looks in /etc/services, using a getservent library call, for the port assignment for the service it requires and issues a bind call to that address number. For example, the service ftp is commonly assigned port 21. The program gives the port of origin and the full destination address (family, port, host address).
Well-known services have permanently assigned ports. TCP ports are logical ports associated with sockets, not physical communications ports. (See /etc/services for standard port assignments.) TCP allocates the port to the calling program. If the program does not ask to bind a port, TCP assigns one when it receives the connect call. The calling program then issues a connect call by identifying its own full address and the full destination address. IP addresses should be retrieved through the gethostbyname call. When the connection completes, the connection is established, the calling program writes and/or reads. If the write exceeds the maximum packet size, TCP breaks it up into separate packets.
The receiving server does much the same thing at the other end, a socket call and a bind call. The receiving server then makes a listen call and an accept, which says, ``Wait for a call, and if one comes in, give it to me.''
The TCP protocol on each machine keeps track of multiple concurrent sessions between machines by assigning numbered ports to the sessions. For example, session A is between machine 1.1, port 4 and machine 1.2, port 2; while session B is between machine 1.1, port 5 and machine 1.2, port 3, and so on.