全部博文(298)
分类: LINUX
2011-04-14 10:27:02
《Unix Network Programming volume 1》, I have done a little modification.
2.2 connect Function
The connect function is used by a TCP client to establish a connection with a TCP server.
#include |
int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); |
Returns: 0 if OK, -1 on error |
sockfd is a socket descriptor returned by the socket function. The second and third arguments are a pointer to a socket address structure and its size, The socket address structure must contain the IP address and port number of the server.
The client does not have to call bind (which we will describe in the next section) before calling connect: the kernel will choose both an ephemeral port and the source IP address if necessary.
In the case of a TCP socket, the connect function initiates TCP's three-way handshake. The function returns only when the connection is established or an error occurs. There are several different error returns possible.
Some systems provide administrative control over this timeout; see Appendix E of TCPv1.
An RST is a type of TCP segment that is sent by TCP when something is wrong. Three conditions that generate an RST are: when a SYN arrives for a port that has no listening server (what we just described), when TCP wants to abort an existing connection, and when TCP receives a segment for a connection that does not exist. (TCPv1 [pp. 246–250] contains additional information.)
Many earlier systems, such as 4.2BSD, incorrectly aborted the connection establishment attempt when the ICMP "destination unreachable" was received. This is wrong because this ICMP error can indicate a transient condition. For example, it could be that the condition is caused by a routing problem that will be corrected.
Notice that ENETUNREACH is when the error indicates that the destination network is unreachable. Network unreachables are considered obsolete, and applications should just treat ENETUNREACH and EHOSTUNREACH as the same error.
We can see these different error conditions with our simple client from Figure 1.5 as follows:
Figure 1.5 TCP daytime client.intro/daytimetcpcli.c
solaris % daytimetcpcli 127.0.0.1
Sun Jul 27 22:01:51 2003
To see a different format for the returned reply, we specify a different machine's IP address (in this example, the IP address of the HP-UX machine).
solaris % daytimetcpcli 192.6.38.100
Sun Jul 27 22:04:59 PDT 2003
Next, we specify an IP address that is on the local subnet (192.168.1/24) but the host ID (100) is nonexistent. That is, there is no host on the subnet with a host ID of 100, so when the client host sends out ARP requests (asking for that host to respond with its hardware address), it will never receive an ARP reply.
solaris % daytimetcpcli 192.168.1.100
connect error: Connection timed out
We only get the error after the connect times out (around four minutes with Solaris 9). Notice that our err_sys function prints the human-readable string associated with the ETIMEDOUT error.
Our next example is to specify a host (a local router) that is not running a daytime server.
solaris % daytimetcpcli 192.168.1.5
connect error: Connection refused
The server responds immediately with an RST.
Our final example specifies an IP address that is not reachable on the Internet. If we watch the packets with tcpdump, we see that a router six hops away returns an ICMP host unreachable error.
solaris % daytimetcpcli 192.3.4.5
connect error: No route to host
As with the ETIMEDOUT error, in this example, connect returns the EHOSTUNREACH error only after waiting its specified amount of time.