UNP notes 2--tcp interface functions

Source: Internet
Author: User

4.1 TCP connection and startup

 

TCP Connection

 

 

4.2 socket Functions
Allocate the smallest unused socket handle
# Include <sys/socket. h>
Int socket (INT family, int type, int Protocol );

Family description
------------------
Af_inet IPv4
Af_inet6 IPv6
Af_local Unix domain Protocol
Af_route route entry interface
Af_key key set Interface

Type description
-------------------
Sock_stream byte stream set Interface
Sock_dgram datagram set Interface
Sock_seqpacket sequential grouping Interface
Sock_raw original Interface

Protocol description
----------------------
Ipproto_tcp TCP
Ipproto_udp UDP
Ipproto_sctp sctp

Af_inet
----------------------
Sock_stream TCP | sctp
Sock_dgram UDP
Sock_seqpacket sctp
Sock_raw IPv4

4.3 connect function
Customers usually use connect to actively establish connections with the server.

# Include <sys/socket. h>
Int connect (INT sockfd, const struct sockaddr * servaddr, socklen_t addrlen); from process to Kernel

The set of interface addresses must contain sin_addr, sin_port, and sin_family.
For TCP interfaces, the three-way handshake of TCP is triggered. So there is the following state conversion.
Closed-> syn_sent-> established.
If the return result of Connect fails, it cannot be used again. You must use a new socket.

4.4 bind Functions
Associate the socket handle with a local or remote address.

# Include <sys/socket. h>
Int BIND (INT sockfd, const struct sockaddr * servaddr, socklen_t addrlen );
0-success,-1-Error

1. The server binds a known port at startup. Servaddr. sin_port = ptons (13); // dedicated to the daytime Service
If you do not call bind to bind a port, when you call connect and listen, the kernel selects a temporary port for the corresponding interface.
The server is recognized by everyone through the well-known port
2. A process can bind a specific IP address to its set interface, but this IP address must be one of the network interfaces of the host.
For a tcp client, an IP address is assigned to the IP datagram sent from this interface.
For TCP servers, only client connections with the IP address can be accepted.
3. If the server does not bind the IP address to its interface, the kernel uses the syn ip address sent by the client as the IP address of the server.
4. The configuration address is specified by inaddr_any, which allows the server to accept customer connections on any network interface.
5. If the kernel selects a temporary port number for the set interface, call getsockname to return the Protocol address.

4.5 listen Function
Do two things:
1 specifies the maximum number of connections that the kernel queues for corresponding interfaces.

2 when a socket creates an interface set, it is assumed to be an active interface set. Is a client interface that will call connect to initiate a connection. Listen converts an unconnected set of interfaces into a passive set of interfaces, indicating that the kernel needs to accept connection requests directed to this set of interfaces. Closed-"listen

# Include <sys/socket. h>
Int listen (INT sockfd, int backlog );
0-succ, 1-fail

Understanding backlog Parameters
The kernel maintains two queues for any given listening set interface:
1. The connection queue has not been completed-the client has sent it to the server, but the server is waiting for the TCP handshake. These interfaces are in syn_rcvd
2. connection queue completed-TCP handshake completed. In the established status

Two queues of TCP three-way handshake and listening set Interface

When the process calls accept, the first entry in the connection queue that has been completed will be returned to the process.

4.6 accept Function
Returns the next completed connection from the completed connection queue header.

Int accept (INT sockfd, struct sockaddr * cliaddr, socklen_t * addrlen );
-1-fail

Cliaddr (client process Protocol address) and addrlen return the connected Protocol address to the customer. If you are not interested in the client, you can write them as null.
Before the call, the integer of * addrlen is set to the length of the Set interface address structure referred to by cliaddr.
* Addrlen integer indicates the exact number of bytes in the interface address structure of the kernel.

The accept return rule is a new description automatically generated by the kernel, representing a TCP connection with the returned customer.
Listener set interface: Socket generation, used in BIND, listen, accept
Connection set interface: accept generation
Distinction between the two:
A server creates only one listening set interface, which exists throughout the lifecycle of the server.
The kernel creates a connection set interface for each client connection accepted by the server process (that is, the three handshakes of the connection have been completed). When the service is completed, the corresponding connection set interface is closed.

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.