0 -- connect function Parsing
# Include <sys/socket. h> int connect (INT socket, const struct sockaddr * address, socklen_t address_len); Return Value 0: Connection established successfully;-1: Connection established failed or other errors occur. See errno;
Network function connect is used to establish a connection between a tcp client and a TCP server.
The socket parameter is the set of interface file descriptors returned by the socket function;
The address parameter is a pointer to the interface address of the server set to be connected;
The address_len parameter is the size of the structure of the set interface address indicated by the parameter address;
Before describing how to use connect and possible problems in actual use, let's take a look at the example of using connect to block connections.
# Include <sys/socket. h> // socket, connect definition # include <ARPA/inet. h> // definition of inet_pton # include <string. h> // memset definition # include <netinet/in. h> // htons, struct sockaddr_in definition # include <errno. h> // errno, strerror definition # include <string> # include <iostream>/***** function return value: **-1: an internal network function call error occurs, see errno; **-2: illegal network address **> = 0: network socket interface file descriptor **/INT do_connect (const STD: string & s_host, const unsigned short usi_port) {// converts the specified struc T sockaddr_in sock_addr; memset (& sock_addr, 0, sizeof (struct sockaddr_in); outputs = af_inet; outputs =: htons (usi_port); Switch (: inet_ton (af_inet, s_host.c_str (), & sock_addr.sin_addr) {Case 0: Return-2; Case-1: Return-1 ;}// create a network set interface file descriptor int sock_fd = :: socket (af_inet, sock_stream, 0); If (sock_fd =-1) {return-1;} // call the connect function to connect to the server if (: connect (sock_fd, (s) Truct sockaddr *) & sock_addr, sizeof (struct sockaddr_in) =-1) {return-1 ;}return sock_fd ;}# define print_errno (ERR) printf ("error (%-15 S % 4d): % s \ n", # err, err, strerror (ERR); int main (INT argc, char ** argv) {If (argc! = 3) {STD: cout <"Usage:" <argv [0] <"host port" <STD: Endl; Return-1 ;} print_errno (econnrefused); print_errno (enetunreach); print_errno (etimedout); int I _ret = do_connect (argv [1], atoi (argv [2]); if (I _ret =-1) {STD: cout <"RET:" <I _ret <"| Eno:" <errno <"| error: "<strerror (errno) <STD: Endl; Return-1;} If (I _ret =-2) {STD: cout <" RET: "<I _ret <" | error: Invalid host "<STD: Endl; Return-2;} STD: cout <" RET: "<I _ret <" | Success | "<I _ret <STD: Endl;: Close (I _ret); Return 0 ;}
1 -- parsing of errno corresponding to connect
4.3 connect function
In the case of a TCP socket, the connect function initiates TCP's three-way handshake (section 2.6 ). the function returns only when the connection is established or an error occurs. there are several different error returns possible.
If the client TCP has es no response to its SYN segment, etimedout is returned. 4.4bsd, for example, sends one SYN when connect is called, another 6 seconds later, and another 24 seconds later (P. 828 of tcpv2 ). if no response is already ed after a total of 75 seconds, the error is returned.
Some systems provide administrative control over this timeout; see Appendix E of tcpv1.
If the server's response to the client's SYN is a reset (RST), this indicates that no process is waiting for connections on the server host at the port specified (I. E ., the server process is probably not running ). this is a hard error and the error econnrefused is returned to the client as soon as the RST is already ed.
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 ipves a segment for a connection that does not exist. (tcpv1 [pp. 246-250] contains additional information .)
If the client's SYN elicits an ICMP "Destination Unreachable" from some intermediate router, this is considered a soft error. the client kernel saves the message but keeps sending SYNS with the same time between each SYN as in the first scenario. if no response is already ed after some fixed amount of time (75 seconds for 4.4bsd), the saved ICMP error is returned to the process as either ehostunreach or enetunreach. it is also possible that the remote system is not reachable by any route in the Local System's forwarding table, or that the Connect call returns without waiting at all.
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 cocould be that the condition is caused by a routing problem that will be corrected.
Notice that enetunreach is not listed in Figure A.15, even when the error indicates that the destination network is unreachable. network unreachables are considered obsolete, and applications shoshould just treat enetunreach and ehostunreach as the same error.
1 -- use tcpdump to observe the connect running status
For connect, we can imagine the situations we encounter in actual use:
- Connect to a server normally (connection successful );
- The listener (server reachable) is not enabled on the port specified by the server );
- If there is routing information but the server does not exist;
- Connect to a server with an inaccessible route;
Connect to a server normally (connection successful );
2 -- non-blocking connect Principle
3 -- non-blocking connect (implemented based on select) 4 -- non-blocking connect (implemented based on epoll)