UNIX's socket programming profile (http:www.fanqiang.com)

xiaoxiao2021-03-06  45

Network programming, that is, writing a program that communicates with other programs via a computer network. In the current mode, in the network program that communicates with each other, one party is called a client program (the other party is called the Server), and most operating systems provide compilation network programs, such as web clients. Browser), web server program, and FTP, Telnet, etc., and application Socket programming interfaces can write their own network communication programs. In the TCP / IP world, the basic model of network communication is as follows: Every communication host has a unique IP address in this network environment, and there are often multiple communication programs on one host, each such program To take up a communication port. Therefore, an IP address, a communication port, can determine the location of a communication program (a program that occupies a port on an IP). The communication program is divided into a service program and a client program. Their difference is that the service program is always passive waiting connection, the service program starts, the initialization will enter the waiting connection status; and after the client starts, send a connection request to the remote service. After receiving the request, the service program has established a connection according to certain rules. After the connection is completed, the two programs have established a virtual data communication link, which can read and write directly from their own socket. Data without care of the actual data link. It will be introduced to two basic programming mode: the customer program and the service program are written. The basic method of customer program writing. Steps: Create a socket → Connection to the remote service program → Read / write data → terminate the connection. Create a socket function with a socket function, this function has three parameters, the first parameter specifies the protocol, such as the AF_INET (IPv4 protocol), AF_INET6 (IPv6 protocol), AF_LOCAL (UNIX domain protocol); the second parameter is a socket type, There are SOCK_STREAMs, SOCK_DGRAMs, SOCK_RAW (original socket); the third parameter is generally 0. Generally, the combination of AF_INET and SOCK_STREAM corresponds to the TCP protocol; the combination of AF_INET and SOCK_DGRAM corresponds to the UDP protocol; AF_INET and SOCK_RAW correspond to the IPv4 protocol. The Socket function returns a set of interface descriptors when the Socket function is successful. Use the remote service program to use the Connect function, this function has three parameters. The first parameter is a socket descriptor; the second parameter is a SockAddr structure, which contains the IP address and port number of the remote service program; the third parameter is the length of the SockAddr structure. After the Connect function call is successful, the socket description word is established with the remote service program, and you can start reading / writing. Read / write data uses read and write functions, both of which are three parameters. The first parameter is a socket description word; the second parameter is a buffer of read / write data; the third parameter is the length of the buffer. After reading, you can call the function close closing the socket, and the parameters are set descriptors. Basic methods written by the service program. Steps: Creating a socket → Binding Settings → Settings Sets Sets to the listening mode, enter the passive connection request status → accept the request, establish a connection → read / write data → terminate the connection. The binding socket is assigned a communication port for use, using a bind function. Call the function Listen backcrut interface to the listening state, then call the Accept function to accept the connection request. If there is no connection request, the Accept function will enable the program to enter the sleep state until the request is woken up by the remote client. Connection Once read / write is the same as the client program.

转载请注明原文地址:https://www.9cbs.com/read-54690.html

New Post(0)