UNP summary Chapter 4 Basic TCP socket programming

Source: Internet
Author: User

1. socket Functions

To execute network I/O, the first thing a process must do is to call the socket function to specify the expected communication protocol type.

# Include <sys/socket. h> int socket (int family, int type, int protocol); // return: non-negative descriptor if successful;-1 if an error occurs

Family indicates the protocol family, and type indicates the socket type. The protocol parameter should be set to a (SEE) protocol type constant value, or set to 0, to select the system default value for the combination of the given family and type

Family constant value of the socket function

Family

Description

AF_INET

AF_INET

AF_LOCAL

AF_ROUTE

AF_KEY

IPv4 protocol

IPv6 protocol

Unix domain Protocol

Router interface

Key set Interface

Type of the socket function

Type

Description

SOCK_STREAM

SOCK_DGRAM

SOCK_SEQPACKET

SOCK_RAW

Byte stream set Interface

Datagram set Interface

Sequential grouping Interface

Original set Interface

Common protocol value of the socket function

Protocol

Description

IPPROTO_TCP

IPPROTO_UDP

IPPROTO_SCTP

TCP transmission protocol

UDP transmission protocol

SCTP transmission protocol

Combination of family and type parameters in socket Functions

 

 

2. connect Function

TCP customers use the connect function to establish a connection with the TCP Server

# Include <sys/socket. h> int connect (int sockfd, const struct sockaddr * servaddr, socklen_t addrlen); // return: 0 if successful,-1 if an error occurs

Sockfd is the socket descriptor returned by the socket function. The remaining two parameters are a pointer to the socket address structure and the size of the structure. The connect function will trigger the TCP three-way handshake process and return only when the connection is established successfully or when an error occurs. The following error occurs:

1) if the TCP client does not receive the response from the SYN packet, the ETIMEDOUT error is returned. For example, when the function is called, the kernel sends a SYN. If there is no response, it waits for 6 seconds and then sends another one. If there is still no response, it waits for 24 seconds for another one, if the response message is not received after 75 seconds in total, this error is returned (varies with the kernel ).

2) If the response time is RST, it indicates that the server host has no process waiting on the port we specified, and the customer immediately returns the ECONNREFUSED error after receiving the RST package.

3 ). if the customer generates an ICMP error "destination unreachable" on the middle router, the customer continues to send SYN in the first case. If no response is received within the specified time, the ICMP error is returned as an EHOSTUNREACH or ENETUNREACH error.

 

 

3. bind Functions

The bind function assigns a local Protocol address to a socket. For Internet protocol, the protocol address is an IP address and a port number.

# Include <sys/socket. h> int bind (int sockfd, const struct sockaddr * myaddr, socklen_t addrlen); // return, success is 0, error is-1

The sockfd parameter is the socket descriptor returned by the socket function. myaddr is a pointer to the Protocol-specific address structure. The third parameter is the length of the address structure. For TCP, you can call the bind function to specify a port, an address, or both of them. You can also choose not to specify either of them:

  • The server binds their well-known ports at startup.
  • A process can bind a specific IP address to its socket, but this IP address must belong to one of the network interfaces of its host.

For IPv4, the common value of the wildcard address is INADDR_ANY. The value is generally 0, which notifies the kernel to select an IP address.

 

4. listen Function

The listen function is called only by the TCP server. It performs two events: 1 ). when function socket creates an interface set, it is assumed to be an active interface set, that is, it is a client interface that will call connect to initiate a connection, the listen function converts an unconnected set of interfaces into a passive set of interfaces, indicating that the kernel should accept connection requests pointing to this set of interfaces. 2 ). the second function of the function specifies the maximum number of connections that the kernel queues for this set of interfaces.

 

# Include <sys/socket. h> int listen (int sockfd, int backlog); // return, success is 0, error-1

To understand the backlog parameters, we need to know that the kernel maintains two queues for any given listening socket:

1). The connection queue is not completed. The tcp three-way handshake between the client and the server was not completed.

2). The connection queue has been completed. The tcp three-way handshake has been completed and is in the ESTABLISHED state.

 

 

Processing of two queues:

  • The backlog parameter of the listen function was set to the maximum value of the sum of two queues.
  • The implementation from Berkeley adds a fuzzy factor to the backlog to multiply it by 1.5 to get the maximum length of the unprocessed queue.
  • Do not define backlog as 0 because different implementations have different interpretations.
  • When the three-way handshake is completed normally (that is to say, no shard is lost and No retransmission is performed), the retention time of any of the uncompleted connection queues is an RTT, the RTT value depends on the specific customer and server.
  • When a customer SYN arrives, if these queues are full, TCP ignores this shard, that is, it does not send RST
  • After the three-way handshake is complete, the data that arrives before the server calls accept should be arranged by the server TCP, and the maximum data volume is the size of the accept buffer for the corresponding connected socket.

 

5. accept Function

The accept function is called by the TCP server and is used to return the next completed connection from the completed connection queue header. If the completed connection queue is empty, the process will be put into sleep (if the socket is the default blocking method)

# Include <sys/socket. h> int accept (int sockfd, struct sockaddr * cliaddr, socklen_t * addrlen); // return: if the operation succeeds, the error is-1

The cliaddr and addrlen parameters return the Protocol address of the connected client. If you are not interested in the client's Protocol address, you can leave it blank, the addrlen parameter is the size of the incoming socket address structure when the function is called. The value returned by the function is the exact number of bytes that the kernel stores in the socket address structure.

If accept succeeds, the returned value is a completely new descriptor automatically generated by the kernel, representing a TCP connection with the returned customer, generally, we call the first parameter of the accept function as the listener socket Descriptor (created by the socket and used as the descriptor of the first parameter of bind and listen), and the return value is the connected socket descriptor.

The accept function returns a maximum of three values: A new socket descriptor or an integer indicated by an error, and the Protocol address of the client process (referred to by the cliaddr pointer) and the size of the address (referred to by the addrlen pointer ).

 

 

6. fork and exec Functions

The fork function (including various variants that may be provided by some systems) is the only way to generate new processes in Unix.

# Include <unistd. h> pid_t fork (void); // return value: 0 for the child process and ID for the child process in the parent process. If the error is-1

The most difficult thing to understand fork is to call it once, but it returns two. The returned value itself indicates whether the current process is a child process or a parent process.

Fork returns 0 in the child process, and returns the ID of the child process in the parent process because one child process has only one parent process, in addition, you can call getppid to obtain the parent process ID in the child process. However, the parent process can have multiple child processes, and there is no way to obtain the ID of the child process in the parent process. If the parent process wants to track the child process, then it must save the sub-process ID after fork returns.

Two typical fork functions:

  • A process creates a copy of itself, and each copy performs its own operations.
  • If a process wants to execute another program, it first calls the fork function to create a copy of itself, and then calls the exec function to replace itself with a new program.

 

The difference between the following exec functions is that

A. Whether the program to be executed is specified by the file name or path name

B. Are the parameters of the new program listed one by one or referenced by a pointer array?

C. Pass the environment of the calling process to the new program or specify the new environment for the new city.

 

#include <unistd.h> int execl (const char *pathname, const char *arg0, ... /* (char *) 0 */ ); int execv (const char *pathname, char *const argv[]); int execle (const char *pathname, const char *arg0, ... /* (char *) 0, char *const envp[] */ ); int execve (const char *pathname, char *const argv[], char *const envp[]); int execlp (const char *filename, const char *arg0, ... /* (char *) 0 */ ); int execvp (const char *filename, char *const argv[]);

In addition, the descriptor opened by the process before exec is called is usually kept open across exec.

 

7. Concurrent Server

Only the status of C/S during the period from accept to fork is shown below

Accept returns the status of the customer/Server

Status of the customer/server after accept is returned

Status of the customer/server after fork is returned

Note that both the listen and connfd descriptors are shared between the parent process and the child process.

In the next step, the parent process closes the connected socket, and the child process closes the listening socket.

Status of the Client/Server after the parent/child process closes the socket

 

 

8. close Function

The Common Unix close function is also used to close the socket and terminate the TCP connection.

# Include <unistd. h> int close (int sockfd); // return: If the success is 0, the error is-1.

The default act of closing a TCP socket is to set the socket to closed, and then immediately return to the calling process. On the concurrent server, fork a sub-process will copy all the descriptors created by the parent process before fork. After the replication is complete, the reference count of the corresponding descriptor will increase by 1. Calling close will reduce the reference count of the descriptor by 1, once the reference count of the descriptor is 0, the kernel closes the socket. If the reference count of the socket descriptor after the close call is still greater than 0, the TCP termination sequence will not be triggered. If you want to send FIN over a TCP connection, you can call the shutdown function.

 

 

9. getsockname and getpeername Functions

The getsockname function returns the local Protocol address associated with a socket. The getpeername function returns the Field Protocol address associated with a socket.

# Include <sys/socket. h> int getsockname (int sockfd, struct sockaddr * localaddr, socklen_t * addrlen); int getpeername (int sockfd, struct sockaddr * peeraddr, socklen_t * addrlen); // return value: 0 is returned for success, and-1 is returned for error.

The two functions are required for the following reasons:
1). On a TCP client that does not call bind, after connect returns a successful result, getsockname is used to return the local IP address and local port number that the kernel assigns to the connection.

2). After calling with port number 0, getsockname is used to return the local port number assigned by the kernel

3) once the connection is established, getpeername can be called to obtain the customer's identity.

4 ). on a TCP server that calls bind with a wildcard IP address, once a connection is established with a customer (accept is returned successfully), getsockname can be used to return the local IP address assigned to the connection by the kernel, in such a call, the socket descriptor parameter must be the descriptor of the connected socket, rather than the descriptor of the listening socket.

5). When a server is executed by a process that has called accept by calling the exec program, the only way it can obtain the customer's identity is to call getpeername.

The inet derivation of is an example. Note that the neutron process is the program file in which the memory image is replaced with the new Telnet server. That is to say, the socket address structure containing the peer address is lost, however, the connected socket descriptor remains open across exec (because of the copy function of the Parent and Child processes ?)

 

 

10. Summary

1). All client server programs start by calling the socket function.

2) The call sequence of the client program is generally socket ---> connect ----> process user input;

3) The call sequence of the server is: socket ---> bind ---> listen ---> accept ---> process user input;

4). The concurrent server creates a process or thread for each customer connection to process the customer's request.

For example:

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.