Socket programming under Linux

  

Network Socket data transfer is a special type of I/O, Socket is also a file descriptor. Socket also has a function called Socket() similar to opening a file. This function returns an integer Socket descriptor. Subsequent connection establishment, data transfer and other operations are implemented through the Socket.

What is a Socket Socket interface is an API for a TCP/IP network. The Socket interface defines a number of functions or routines that programmers can use to develop applications on a TCP/IP network. To learn TCP/IP network programming on the Internet, you must understand the Socket interface. The Socket interface designer first put the interface in the Unix operating system
. If you understand the input and output of a Unix system, it is easy to understand the Socket. The Socket data transmission of the network is a special type of I/O, and Socket is also a file descriptor. Socket also has a function called Socket() similar to opening a file. This function returns an integer Socket descriptor. Subsequent connection establishment, data transfer and other operations are implemented through the Socket. There are two commonly used Socket types: Streaming Socket (SOCK_STREAM) and Datagram Socket (SOCK_DGRAM). Streaming is a connection-oriented Socket for connection-oriented TCP service applications; Datagram Socket is a connectionless Socket that corresponds to a connectionless UDP service application.

Socket Build In order to create a Socket, the program can call the Socket function, which returns a handle similar to the file descriptor. The socket function prototype is: int socket(int domain, int type, int protocol); domain specifies the protocol family to use, usually PF_INET, which represents the Internet Protocol family (TCP/IP protocol family); type parameter specifies the type of socket: SOCK_STREAM Or SOCK_DGRAM, the Socket interface also defines the original Socket (SOCK_RAW), allowing the program to use the lower layer protocol; the protocol is usually assigned the value of ""0". The Socket() call returns an integer socket descriptor that you can use later in the call. A Socket descriptor is a pointer to an internal data structure that points to the descriptor table entry. When the Socket function is called, the socket executable will create a Socket. In fact, "Building a Socket" means allocating storage space for a Socket data structure. The Socket Executor manages the descriptor table for you. A network connection between two network programs includes five types of information: communication protocol, local protocol address, local host port, remote host address, and remote protocol port. These five kinds of information are included in the Socket data structure.

Socket Configuration After returning a socket descriptor through a socket call, the socket must be configured before using the socket for network transmission. The connection-oriented socket client saves local and remote information in the socket data structure by calling the Connect function. Clients and servers with no sockets and server-oriented sockets configure local information by calling the bind function. The Bind function associates the socket with a port on the machine, and then you can listen for service requests on that port. The Bind function prototype is: int bind(int sockfd, struct sockaddr *my_addr, int addrlen); Sockfd is the socket descriptor returned by calling the socket function, and my_addr is a sockaddr type that contains information such as the local IP address and port number. Pointer; addrlen is often set to sizeof(struct sockaddr). The struct sockaddr structure type is used to store socket information: struct sockaddr { unsigned short sa_family; /* address family, AF_xxx */char sa_data[14]; /* 14-byte protocol address */}; sa_family is generally AF_INET, Represents the Internet (TCP/IP) address family; sa_data contains the IP address and port number of the socket. There is also a structure type: struct sockaddr_in { short int sin_family; /* address family */unsigned short int sin_port; /* port number */struct in_addr sin_addr; /* IP address */unsigned char sin_zero[8]; /* Fill 0 to keep the same size as struct sockaddr*/}; This structure is more convenient to use. Sin_zero is used to fill the sockaddr_in structure to the same length as struct sockaddr, which can be set to zero with the bzero() or memset() function. Pointers to sockaddr_in and pointers to sockaddr can be converted to each other, which means that if the required parameter type of a function is sockaddr, you can convert a pointer to sockaddr_in to a pointer to sockaddr at the time of the function call; or vice versa. When using the bind function, you can use the following assignment to automatically obtain the local IP address and randomly obtain an unoccupied port number: my_addr.sin_port = 0; /* The system randomly selects an unused port number */my_addr. Sin_addr.s_addr = INADDR_ANY; /* Fill in the local IP address*/By setting my_addr.sin_port to 0, the function will automatically select an unoccupied port for you to use. Similarly, by setting my_addr.sin_addr.s_addr to INADDR_ANY, the system automatically fills in the local IP address. Note that when using the bind function, you need to convert sin_port and sin_addr to network byte first order; sin_addr does not need to convert. Computer data storage has two byte priority orders: high byte first and low byte first. Data on the Internet is transmitted on the network in high-order byte order, so for machines that store data internally in low-order byte-first mode, data needs to be converted when transmitting data on the Internet, otherwise data inconsistency will occur. Here are a few byte order conversion functions: ·htonl(): Convert 32-bit values ​​from host byte order to network byte order ·htons(): Convert 16-bit values ​​from host byte order to network word Sections ·ntohl(): Convert 32-bit values ​​from network byte order to host byte order ·ntohs(): Convert 16-bit value from network byte order to host byte order Bind() function succeeded Returns 0 when called; returns "-1" when an error occurs and sets errno to the corresponding error number. It should be noted that when calling the bind function, generally do not set the port number to a value less than 1024. Since 1 to 1024 is the reserved port number, you can select any port number that is greater than 1024 that is not occupied.

Connection Establishing a connection-oriented client program uses the Connect function to configure the socket and establish a TCP connection with the remote server. The function prototype is: int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); Sockfd Is the socket descriptor returned by the socket function; serv_addr is a pointer containing the remote host IP address and port number; addrlen is the length of the remote geological structure. The Connect function returns -1 when an error occurs and sets errno to the corresponding error code. There is no need to call bind() for client program design, because in this case, you only need to know the IP address of the destination machine, and the client does not need to care about which port to connect with the server. The socket executable automatically selects one for your program. Occupied port and notify you when the program data is broken.

Copyright © Windows knowledge All Rights Reserved