Study Notes on Unix Network Programming Based on TCP socket programming

Source: Internet
Author: User

Study Notes on Unix Network Programming Based on TCP socket programming

1. socket Functions

Int socket (int family, int type, int protocol)

Returns a socket descriptor. Error returned-1

Family indicates the protocol family. Generally, IPv4 is AF_INET and IPv6 is AF_INET6.

Type specifies the socket type, and the byte stream is SOCK_STREAM. The datagram is SOCK_DGRAM.

Generally, the combination of family and type can uniquely determine a socket type. So we can set protocol to 0.

Sometimes, in some special cases, the combination of family and type is not both valid. In this case, we need to specify some special values for protocol.

2. connect Function

Int connect (int sockfd, const struct sockaddr * servaddr, socklen_t addrlen );

Connect to the server, where servaddr is the server address.

For TCP sockets, connect triggers three handshakes.

As you can see from the past, when the client receives a SYN response from the server, the connect function returns. If the SYN sent by the client fails, or an ACK error in the response will cause an error in the connect function. 0 is returned for success,-1 is returned for error, and errno is set.

Note: If a connect error occurs, you cannot connect again directly. You must first close the socket and then re-connect the socket.

3. bind Functions

Int bind (int sockfd, const struct sockaddr * servaddr, socklen_t addrlen)

0 is returned for success, and-1 is returned for error.

Bind a local socket address to the specified socket.

(1) generally, the server needs to bind a public port number, while the server usually binds an Ip address in INADDR_ANY, meaning that when accept is used, the kernel selects a local IP address from the local Ip address to assign a value. This is very influential when a machine has multiple network interfaces.

Generally, the machine has only one network interface, so we also use this method because we do not need to write the local IP address (hard encoding) of the server. This write makes our program portable.

(2) connect directly after the socket function of the client, without bind, because we usually do not need to specify the Ip address and port number of the client. Let the kernel automatically assign values.

(3) INADDR_ANY in IPv4 is usually 0, so when we assign a value to it, the following format is used:

Servaddr. sin_addr.s_addr = htonl (INADDR_ANY );

Because sin_addr is a struct, we use sin_addr.s_addr to assign values as integers.

4. listen Function

Int listen (int sockfd, intbacklog)

0 is returned for success, and-1 is returned for error.

Note that the listen here is not the meaning of the listener we usually understand, because the socket is not blocked here, but is blocked in accept.

Listen only performs two tasks:

(1) set the socket created by the socket function as a passive socket. Because the socket function creates active sockets by default, active sockets: connect is required for active connections.

(2) specifies the maximum number of connections that the kernel should queue.

How does the kernel queue connections?

The kernel maintains two queues. The connection queue has not been completed, and the connection queue has been completed.

Unfinished Connection queue: After the customer SYN arrives, it is placed at the end of the unfinished connection queue.

Connection queue completed: After the customer completes three handshakes, it is placed at the end of the completed queue.

Then the process calls accept and returns the first entry from the completed queue to the process.

Question here? Isn't accept blocking always on the server side? Why does the process call accept?

This is because a connection occurs when multiple clients reach a connection almost at the same time, because when we write the server program, the accept is written in a loop, therefore, when a customer's SYN arrives, the accept may not be executed at this time, so it is said that the process will wait until the accept is called. That is to say, the accept will be blocked only when the complete connection queue is empty.

Note that the backlog mentioned here is the sum of two queues. However, in actual conditions, the number of queues allowed by the kernel is slightly greater than this value.

5. accept Function

Int accept (int sockfd, struct sockaddr * cliaddr, socklen_t * addrlen)

A descriptor is returned successfully. Error-1 is returned.

Accept the customer connection. If there is data in the completed connection queue, read the queue header and return a connected socket descriptor. If the complete connection queue is empty, it is blocked.

A connected socket descriptor is returned for success, and a negative value is returned for failure.

Note: The third parameter is the integer address. Because the accept function is a socket from the kernel. If the program is not interested in the socket address of the client, you can set both parameters to NULL.

A server usually has only one listening socket, and a connected socket is created for each customer.

6. getsockname and getpeername

Int getsockname (int sockfd, struct sockaddr * addr, socklen_t * addrlen)

Returns the local socket address associated with the socket descriptor sockfd.

Int getpeername (int sockfd, struct sockaddr * addr, socklen_t * addrlen)

Returns the end-to-end socket address associated with the socket descriptor sockfd.

Obviously, both functions obtain the socket address from the kernel, so the third parameter is the integer address.

Note:

(1) generally, the client does not have bind. Therefore, getsockname/getpeername can be called only after connect.

(2) After the bind port on the server, call getsockname to obtain the port number.

Generally, bind on the server is a wildcard address. Therefore, you cannot obtain the associated IP address of the listener socket descriptor, but the IP address associated with the connected socket descriptor.

(3) POSIX allows getsockname to be called on a socket without bind. Therefore, this function is suitable for any opened socket Descriptor (that is, the socket descriptor returned by the socket function is called an opened socket descriptor ), but not necessarily what is output.

Insert knowledge:

1. the socket functions are the same. If a success returns 0/Descriptor and a Failure Returns-1. Therefore, the conditions for successful judgment are the same.

2. RST group. RST group is a TCP group sent by TCP when an error occurs.

Conditions for RST generation:

(1) When a SYN arrives at a port and the local machine does not listen to the program on the port, the local machine sends an RST.

(2) TCP wants to cancel a connection.

(3) TCP receives a group with a nonexistent connection. That is, if a client is not connected, the server sends data to the server. Then the server sends the RST to the client.

In fact, RST means to reconnect the other party.

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.