UNIX Network Programming Reading Notes: Basic TCP Interface Programming

Source: Internet
Author: User

 

Compile a complete set of interface functions required by TCP client and server programs:

The family parameter indicates the protocol family, which is also known as the Protocol domain ). Therefore, some books declare the following:

AF_xxx and PF_xxx:

AF _ prefix table address family, and PF _ prefix indicates the protocol family. In history, there has been an idea that a single protocol family can support multiple address families. The PF _ value is used to create a set of interfaces, and the AF _ value is used to set the interface address structure. However, the Protocol families that support multiple address families have never been implemented, and the header file <sys/socket. h> the PF _ value defined for a given protocol is always equal to the AF _ value of this Protocol..

If the connection fails, this interface is no longer available and must be closed. We cannot call the connect function again for such an interface.When function connect is called cyclically and the IP addresses of the specified host are tried until a successful connection fails, the current set of interface description must be closed and the socket must be called again.

Calling listen causes the set of interfaces to switch from the CLOSED status to the LISTEN status.

(2) The backlog parameter specifies the maximum number of connections that the kernel should queue for corresponding interfaces.

To understand the backlog parameters, we must realize that the kernel maintains two queues for any given listening set interface:

(1) incomplete connection queue (incomplete connection queue). Each of these SYN segments corresponds to one of the following: the connection queue has been sent by the customer and arrived at the server, the server is waiting to complete the TCP three-way handshake process. These interfaces are in the SYN_RCVD status.

(2) The connection queue has been completed. Each customer who has completed the TCP three-way handshake corresponds to one of them. These interfaces are in the ESTABLISHED status.

Listening socket)(Created by the socket and then used as the description of the first parameter of bind and listen), it is called the return value as the connected socket). It is very important to distinguish these two sets of interfaces. A server generally creates only one listening set interface, which always exists during the life cycle of the server. The kernel creates a connection set interface for each client connection accepted by the server process (that is, the TCP three-way handshake process has been completed ). When the server completes the service for a given customer, the corresponding connected interfaces are closed.

This function returns a maximum of three values: one may be a new interface description or an integer indicating 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 ). If we are not interested in the returned client Protocol address, we can set both cliaddr and addrlen as null pointers.

Fork function: http://www.cnblogs.com/nufangrensheng/p/3509492.html.

Exec function: http://www.cnblogs.com/nufangrensheng/p/3510821.html.

The usual UNIX close function is also used to close the set of interfaces and terminate TCP connections.

All customers and servers start from the socket call and return a set of Interface Description words. The client then calls connect, and the server calls bind, listen, and accept. Standard close functions are usually used to close the set of interfaces.

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.