Computer data store has two bytes priority sequence: high byte priority and low byte priority. The Internet is transmitted on the network in a high byte priority order, so it is necessary to transfer data on the Internet in the Internet to the Internet. The first structural type we have to discuss is: struct sockaddr, this type is used to save Socket information: struct sockaddr {unsigned short sa_family; / * address family, AF_XXX * / CHAR SA_DATA [14]; / * 14 bytes The protocol address * /}; sa_family is generally AF_INET; SA_DATA contains the IP address and port number of the socket. There is also a structural type: strunt 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 * /} with struct sockaddr; this structure is more convenient. SIN_ZERO (which is used to fill the SockAddr_in structure to the same length as Struct SockAddr) should be placed zero with the Bzero () or MEMSET () function. Poinpectdddr_in's pointer and pointer to SockAddr can be converted to each other, which means that if the parameter type required for a function is SockAddr, you can convert a pointer to the SockAddr_in when the function call is called to point to the socketdr pointer; or the opposite. SIN_FAMILY is usually assigned AF_INET; SIN_PORT and SIN_ADDR should be converted into network byte priority; while SIN_ADDR does not need to be converted. Let's discuss several byte sequential conversion functions: htons () - "Host to NetWork Short"; htonl () - "Host to NetWork Long" () - "NetWork to Host Short"; NTOHL () - - "NetWork to Host Long" Here, h indicates "host", n means "network", and s represents "short", and L means "long". Open the Socket descriptor, establish the binding and establish the connection socket function original to: int Socket (int Domain, int type, int protocol); Domain parameter specifies the type of socket: SOCK_STREAM or SOCK_DGRAM; protocol usually assigns "0". Socket () call returns an integer socket descriptor that you can use it later. Once returns a Socket descriptor via the Socket call, you should associate this socket with a port on you (often call the function when you design the server-side program. You can then listen to the service request at the port And the client generally does not need to call the function).
Bind function original is: int bind (int Sockfd, intectRlen); sockfd is a socket descriptor, my_addr is a pointer to the SockAddr type that contains information such as the native IP address and port number; AddRlen It is set to sizeof (struct sockaddr). Finally, the point to the bind function is that you can automatically get the native IP address and randomly get a port number without the occupied port number: my_addr.sin_port = 0; / * System randomly selects one unused Port number * / my_addr.sin_addr.s_addr = INADDR_Addr; / * Fill in the native IP address * / by setting my_addr.sin_port, the function will automatically select an unidentified port to use. Similarly, by placing my_addr.sin_addr.s_addr is set to INADDR_Any, the system will automatically fill in the native IP address. The bind () function returns 0 when successfully called; return "-1" when an error is encountered and set Errno to the corresponding error number. Also note that when the modulation function is usually, do not set the port number to a value of less than 1024, since 1 to 1024 are reserved port numbers, and you can use any of the port numbers that are not occupied in 1024. The Connect () function is used to create a TCP connection with the remote server, its function prototype: int connection_addr, int addrlen; sockfd is the Sockt descriptor of the destination server; serv_addr is an IP containing the desertive machine IP The address and port number of the port number. Returns -1 when an error is encountered, and the corresponding error code is included in Errno. The client program design does not need to be called bind (), because in this case, you only need to know the IP address of the machine, and which port is connected to the server and the server does not need to care, the kernel will automatically select an unbeatable port for The client is used. Listen () - Monitoring Is Service Request In the server-side program, after the Socket is bundled with a certain port, you need to listen to the port to process the service request arriving. INT Listen (int Sockfd, int Backlog); SockFD is the socket descriptor returned by the socket system call; Backlog specifies the maximum number of requests allowed in the request queue, and the access request will wait for Accept () in the queue (hereinafter) . BACKLOG is limited to the number of requests waiting for the service in the queue, and most system defaults to 20. Returns -1 when Listen encounters an error, Errno is set to the corresponding error code. Therefore, the server-side program typically performs function calls in the following order: socket (); bind (); listen (); / * accept () goes here * / accept () - Connect the service request for the port. This connection request will queue the waiter Accept () when a client attempts to connect with the server listening to the server. The Accept () function returns a new Socket descriptor to be used for this new connection by calling the accept () function.
The server can continue to listen over the previous socket, while the data Send () (send) and RECV () operation can be performed on the new Socket descriptor: int access (int sockfd, void * addr, int *) AddRlen; SockFD is the listened socket descriptor, and addr is usually a pointer to the SockAddr_in variable, which is used to store the information of the host of the connection request service (a host issues the request from a port); AddRTEN usually For a pointer variable pointing value of sizeof (struct socaddr_in). Error returns one -1 and sets the corresponding errno value. Send () and RECV () - Data Transmission These two functions are data transmission on the connection-oriented Socket. Send () function original is: int send (int Len, int flags); SOCKFD is the socket descriptor you want to transfer data, and the MSG is a pointer to the data to send. Len is the length of data in bytes. Flags typically be set to 0 (with the usage of this parameter can be referred to the Man Manual). Char * msg = "Beej Was Here!"; int LEN, BYTES_SENT; ... ... LEN = Strlen (MSG); Bytes_Sent = Send (Sockfd, MSG, Len, 0); ... ... Send ) The function returns the number of bytes actually sent, which may be less than the data you want to send. So you need to measure the return value of Send (). This situation should be processed when the send () return value is mismatched with the LEN. The RECV () function original is: int RECV (int LEN, Unsigned INT FLAGS); SockFD is a socket descriptor that accepts data; BUF is a buffer that stores receiving data; LEN is the length of the buffer. Flags is also set to 0. RECV () returns the number of bytes that is actually received, or when an error occurs, returns the corresponding Errno value. Sendto () and RECVFROM () - Data transfer in the data report in the unconnected datagram socket mode, because the local socket does not establish a connection with the remote machine, the address should be specified when sending data, Sendto ( The function original is: int sendto (int Len, unsigned int flag, const struct socöta); INT tolen; the function is more than two parameters than the send () function, TO means the style The IP address and port number information are often assigned to SizeOf (Struct SockAddr). The SendTo function also returns the actual data byte length or returns -1 when an error occurs. The RECVFROM () function original is: int RECVFROM (int LEN, Unsigned Int Flags, Struct SockAddr * from, INT * FROMLEN); from FROMLEN is a Struct SockAddr type variable, the variable holds the IP of the source Address and port number. Fromlen is always set to SIZEOF (Struct SockAddr). When Recvfrom () returns, the fromLOM contains the number of data bytes actually stored in the FROM.