Socket programming foundation under Linux
Author: Northeastern University Software Technology R & D Center in sums King evaluation: the station Date: 2002-05-22 Description: Source:
1. Introduction Linux's rise can be said to be a miracle created by Internet. As a free free software that completely open its original code, Linux is compatible with a variety of UNIX standards such as POSIX, UNIX SYSTEM V, and BSD UNIX, etc.), multitasking operating systems with complex kernels. In China, with the popularity of the Internet, a group of Linux enthusiasts that mainly composed of students and ISP technicians in higher colleges and universities have grown. More and more programming enthusiasts have gradually love this excellent free software. This article describes the basic concepts and function calls of Socket under Linux. 2, what is SocketSocket (socket) is a method that communicates through standard UNIX file descriptors and other program. Each socket is used in one and a semi-related description: {protocol, local address, local port} is represented; a complete socket uses a related description: {protocol, local address, local port, remote address, remote port }, Each socket has a local socket that is allocated by the operating system. 3, Socket's three types (1) Flow socket (Sock_Stream) stream socket provides reliable,-facing communication flow; it uses TCP protocols to ensure the correctness and order of data transfer. (2) Data News Socket (SOCK_DGRAM) Data Supply Settings Define a connectionless service, data is transmitted by transfers with each other, and does not guarantee reliable, no errors. It uses a DBR Agreement UDP (3) Original Socket Original Socket Allows direct access to the underlying protocol such as IP or ICMP, which is powerful but more inconvenient, mainly for some protocols. 4. Send data by using the socket to send data to the flow socket system to send data. 2. For the data settlement, you will need to add a message header first, then call the sendto () function to send the data. 5, SOCKET data structure (1) struct sockaddr {// for storage socket address unsigned short sa_family; // address type char sa_data [14]; // 14 byte protocol address}; (2) Struct SockAddr_in {// IN Represents InternetShort Int Sin_Family; // Internet Protocol UNSIGNED SHORT INT SIN_PORT; / / Port number, must be a network byte order Struct in_addr sin_addr; // Internet address, must be network byte order unsigned char sin_zero ; // Add 0 (same size as Struct SockAddr}; (3) Struct in_addr {unsigned long s_addr;}; 6, network byte order and its conversion function (1) network byte sequence each machine internal pair of variables The byte storage order is different, and the data transmitted by the network is to unify the order. So the internal byte indicates the order of the order and the network byte order, must be converted to the data, from the program's portability requirements Even if the internal byte representation of this unit is the same as the network byte order, the data conversion function should be called before the transmission data, so that the program can be implemented correctly after being ported to other machines. Real conversion is not converted by system functions Decide.
(2) Conversion function * Unsigned short int htons (unsigned short int hostshort): The host byte sequence is converted into network byte order, and the unsigned short-operated 4Bytes * unsigned long int htonl (unsigned long int hostlong): The host byte sequence is converted into a network byte order, and the unsigned long-sized SHORT INT NTOHS (UNSIGNED SHORT INTSHORT): The network byte sequence is converted into the host byte order, and operate 4bytes without symbolic short-type * Unsigned long int NTOHL: Network byte sequence into host byte sequence, operates without symbolic long-scale 8Bytes Note: The above function prototypes are defined in Netinet / In.H, and IP address is converted. A function converts the string IP address represented by the digital point form with a 32-bit network byte sequence IP address (1) unsigned long int inet_addr (const char * cp): This function puts a number and point representation The string of the IP address is converted into an unsigned long, such as: struct sockaddr_in inaina.sin_addr.s_addr = inet_addr ("202.206.17.101") This function is successful: Return the conversion result; return constant inaddr_none when failing, this constant = -1, binary unsigned integer-1 is equivalent to 255.255.255.255, this is a broadcast address, so when I call INER_ADDR () in the program, it must be artificially failed to process the call failed. Since the function cannot process the broadcast address, the function inet_aton () should be used in the program. (2) INT inet_aton (constra * inv): This function converts the IP address of the string into binary form; return 1 when it is successful, otherwise returns 0, the converted IP address is stored in Parameter INP. (3) CHAR * INT_NTOA (STRUCT IN-AddR in): Convert 32-bit binary IP addresses to the IP address in the digital point form, and the result is returned in the function return value, and returns a pointer to the string. 8. The byte processing function socket address is multi-byte data, not ending with empty characters, which is different from the strings in the C language. Linux provides two sets of functions to handle multi-byte data, and a group starts with B (Byte), is a function compatible with the BSD system, and the other in MEM (memory) is a function provided by ANSI C. Functions starting with B include: (1) Void Bzero (Void * S, INT N): Set the first n byte of the memory specified by the parameter S to 0, usually it is used to clear the socket address.
(2) Void Bcopy (const void * src, void * dest, int N): Copy the memory area specified from the parameter src to the specified number of bytes to the memory area specified by the parameter DEST. (3) INT BCMP (const void * s1, const void * s2, int N): The previous N byte content of the memory area specified by the parameter S1 and the parameter S2 specified, if the same, return 0, otherwise returns Non 0. Note: The prototype of the above functions is defined in Strings.h. The functions starting with the MEM are: (1) Void * Memset (Void * S, INT C, SIZE_T N): Set the previous N bytes of the memory area specified by the parameter S as the contents of the parameter C. (2) Void * Memcpy (Void * Dest, const void * src, size_t n): function with bcopy (), difference: Function bcopy () can handle the area specified by parameter src and parameter DEST, Memcpy ( It is not possible. (4) INT MEMCMP (const void * s1, const void * s2, size_t n): The front n byte content of the comparison parameter S1 and the parameter S2 specifies the area, if the same, returns 0, otherwise returns non-0. Note: The prototype of the above function is defined in string.h. 9. Basic socket function (1) socket () # include
(2) Function Connect () # include
Usually the TCP protocol logs this message after receiving this ICMP message, then sends a SYN data segment several times. After all sending are failed, the TCP protocol checks this ICMP message, and the function is returned. Note: ICMP: Internet Message Control Protocol. The operation of the Internet is mainly controlled by the Internet's router, the router completes the transmission and reception of the IP packet. If an error occurs when sending a packet, the router uses the ICMP protocol to report these errors. The ICMP packet is encapsulated in the data section of the IP packet, and its format is as follows: Type Code Check and Data 0 8 16 24 31 Type: Indicates the type of ICMP packet. Code: Provide further information for ICMP packets. Check: Provides checksum for the content of the entire ICMP packet. The ICMP packet has the following types: (1) Destination is not reachable: a, the destination host is not running; B, the destination address does not exist; C, the entry in the routing table corresponds, so the router cannot find the host Routing. (2) Timeout: The router minimizes the survival time (TTL) domain of the received IP packet, if the value of this domain becomes 0, the router discards this IP packet and sends this ICMP message. (3) Parameter error: Send when there is an invalid domain in the IP packet. (4) Redirection: Notify the host of a new path. (5) Echo request, Echo Answer: These two message terms test the host to arrive. The requester sends an Echo request ICMP packet to the destination host, and the destination host is returned after receiving this ICMP packet, returns the ECHO to answer the ICMP packet. (6) Time Stamp Request, Time Stamp Answer: ICMP Protocol Use these two messages to get the current time of its clock from other machines. During the call function Connect, when the client TCP protocol sends the confirmation of the SYN data segment, the TCP state is converted to the SYN_SENT state by the closed state. After receiving the confirmation of the SYN data segment, the TCP state is converted to the Established state, the function Successfully returned. If the call function Connect fails, you should use Close to close this socket descriptor, and this socket descriptor cannot be used again to call the function Connect.
Note: TCP Protocol Status Conversion Map: Passive Open Close Active Open (Create TCB) (Delete TCB) (Establish TCB, Send SYN) Receive SYN Send (Send SYN, ACK) Receive SYN ACK (No Action) Receive SYN ACK Receive SYN, ACK (No Action) Close (Send FIN) Close Receive FIN (Send FIN) Receive Fin Receive Fin ACK (No Action) (Send ACK) Close (Send FIN) CLOSE Receive the ACK receiving a FIN ACK Receive the ACK of the FIN (Download ACK) (No Action) (No Action) 2MSL Timeout (Delete TCB) (3) Function Bind () Function Bind Binds the local address with the socket, Definitions as follows: #include
(4) Specify the client's IP address and connection port number: indicating that the client communicates with the specified network device interface and port number. (5) Specify the client's IP address: indicating that the client communicates with the specified network device interface and port number, the system automatically selects an unused port number. Generally only used when there are multiple network device interfaces in the host. We generally do not use a fixed client port number on the client unless you must use. There is a unfavorable port number using a fixed port number on the client: (1) The server performs active shutdown operation: The server will finally enter the TIME_WAIT status. When the client is connected to this server again, the same client port number is still used, so this connection is exactly the same as the previous connection, but one, for the Time_Wait state, did not disappear So this connection request is rejected, and the letter CONNECT returns in an error. The error type is an ECONNREFUSED (2) client execution active shutdown operation: the client will finally enter the TIME_WAIT status. When this client program is executed again, the client will continue to bind this fixed client port number, but because the previous connection is in the Time_Wait state, there is no disappearance, the system will find that this port number is still occupied, so this time The binding operation fails, the function bind returns to Error, the error type is EADDRINUSE. (4) Function listen () function Listen converts a socket to a listening socket, defined as follows; #include
The parameter backlog specifies the maximum length of the completion of the connection queue of a certain listening sleeve, indicating that the maximum number of non-received connections that this socket can receive. If the completion queue of the listening socket is full when a client's SYN data segment arrives, the TCP protocol will ignore this SYN data segment. For the SYN data segment that cannot be received, the TCP protocol does not send the RST data segment, and (5) The function accept () function accept (5) The function accept () function accept (5) The function accept () function accept receives a TCP connection that has been established from the completion of the listening socket. If the completion of the connection queue is empty, then this process sleeps. #include