How to make Socket programming

zhaozj2021-02-08  234

How to make a socket programming process communication is initially originated from a stand-alone system. Since each process runs within its respective address range, in order to ensure both interference between the two mutual communication, cooperative work, operating system Provide corresponding facilities, such as Pipes (PIPEs), Named Pipes and Soft Interrupt Signals (Singal), Message, Shared Memory and Signals Semaphore, etc., but are limited to communication between the machine processes. Network process communication should be solved by communication issues between different host processes (which can be used as the special cases of the same machine process). To this end, the first thing to solve is the problem of network process identification. On the same host, different processes can be uniquely identified by the process number (PID). However, in a network environment, the process numbers independently allocated by each host cannot uniquely identify the process. For example, host A gives a process number 5, and there is also a process in the B host, so this sentence is meaningless. Second, the network protocol supported by the operating system has different ways of working in different protocols, and the address format is different. Therefore, network process communication also solves the identification problem of multiple protocols. In order to solve the above problems, the TCP / IP protocol introduced the following concepts. A communication port that can be named and addressed in a port network is a resource that the operating system is assignable. According to the description of the OSI seven-layer protocol, the maximum difference between the transport layer and the network layer is that the transport layer provides process communication capabilities. In this sense, the end address of network communication is not only a host address, but also an identifier that can describe the process. For this purpose, the TCP / IP protocol proposes the concept of protocol ports to identify the process of communication. The port is an abstract software structure, including some data structures and I / O buffers. After the application is to establish a connection with a port through the system call to a port, the data transmitted to the port is received by the corresponding process, and the data transmitted to the transport layer is output from the port. In the implementation of the TCP / IP protocol, port operations are similar to a general I / O operation, and the process acquires a port, which is equivalent to obtaining a local unique I / O file, and can be accessed with a general read and write. Similar to the file descriptor, each port has an integer descriptor called port numbers to distinguish different ports. Due to the two protocols of the TCP / IP transport layer TCP and UDP are two fully independent software modules, their respective port numbers are independent of each other. If the TCP has a 255th port, UDP can also have a 255th port, and both do not conflict. The allocation of the port number is an important issue. There are two basic allocations: the first kind of global allocation This is a centralized allocation method, and a recognized central institution needs full distribution according to the user, and will be published in the public The second is local allocation, also known as dynamic connection, that is, when the process needs to access the transport layer service, apply to the local operating system, and the operating system returns the unique port number of the local area, and the process is called by the appropriate system. The port is connected (bind). The TCP / IP port number is allocated in two ways. TCP / IP divides the port number into two parts, a small amount of retention port, assigned to the service process in a global manner. Therefore, each standard server has a globally recognized port called Quality, even in different machines, its port number is also the same. The remaining port is allocated in a local manner. TCP and UDP are specified, and less than 256 ports can serve as a reserved port. The two processes in address network communication are on two different machines. In an interconnect network, two machines can be located in different networks, which are connected to the Internet access device (gateway, bridge, router). Therefore, a three-level addressing is required. 1.

A host is connected to multiple networks, and a specific network address must be specified; 2. On the Internet, a host should have its unique address; 3. Each process on the Men's host should have a unique identifier on the host. The host address is IP, don't say more. The unique identifier of the process is the sixteenth integer port number. The network byte sequence different computers are different from the order of multi-word values, and some machines store low bytes in the start address, and some are the opposite. In order to ensure the correctness of the data, you need to specify the network byte order in the network protocol. TCP / IP protocol uses 16-bit integers and 32-bit integers, they all include in the header files of the protocol. Communication link between two processes is called a connection. Connected to internal performance is some buffer and a set of protocol mechanisms, externally externally reserved the reliability of no connection. Semi-related summary, the network can be used in a three-way group in the global single standard: (protocol, native address, local port number) such a three-way group, called one semi-correlated, he specified Every half. All relevant a complete network process communication requires two processes and can only use the same high-level protocol. That is to say TCP and UDP cannot communicate. So a complete network process communication requires a five-way group to identify: (protocol, local address, local port number, remote address, far port number) such a five-way group called a full correlation. In TCP / IP network applications, the main mode of the two process interactions of communication is the client / server mode, that is, the client will issue a request to the server, and the server will provide the corresponding service client / server mode based on the request. The following two points: First, the cause of the network is to be soft, hardware resources, calculation capabilities and information in the network, need to be shared, resulting in a host providing service with a number of resources, less resources, less resources, etc. . Second, the network process communication is completely asynchronous. There is neither a parent-child relationship between the processes of each other, and does not share the memory buffer, so there is a mechanism to establish a connection between the process of hoping to communicate, for the data Switch provides synchronization, which is based on the client / server mode TCP / IP. The client / server mode takes a proactive request mode in the operation: First, the server party will start and provide the appropriate service according to the request: 1. Open a communication channel and inform the local host, it is willing to accept the customer request on a well recognized address port (Zhou Zhi, such as HTTP 80). 2. Waiting for the customer request to reach this port. 3. Receive a repetitive service request, process the request and send a response signal. Receive concurrent service requests, to activate a new process to process this customer request. The new process handles this customer request, does not need to respond to other requests. After the service is complete, turn off the new process with the customer's communication link and terminate. 4. Returns the second step, waiting for another customer request 5. Close the server. Customer party: 1. Open a communication channel and connect to the specific port of the host where the server is located. 2. Send a service request message to the server, wait and receive a response; continue the request. 3. The communication channel is turned off after the request is completed. From the above description process, you can know: 1. The role of the client and the server process is non-symmetrical. So the coding is different. 2. The service process is generally started before the customer request. As long as the system is running, the process has always been present until it is normally or forced to terminate. In UNIX World, there are two types of network application programming interfaces: BSD socket socket and system v TLI. Since Sun adopts the BSD system that supports TCP / IP, TCP / IP's application has greater developed its network. Application Programming Interface Socket has become a standard in network programming. And I have already entered the world of MS.

The Socket of TCP / IP provides the following three types of sockets 1. Flow socket (socket_stream) provides a connection, reliable data transfer service, no error in data, no repetitive transmission, and receiving in the sequence of transmission. Internal flow control, avoiding data flow overrun; data is considered to be byte stream, no length limit. The FTP protocol is a streaming socket. 2. Data reporting socket (socket_dgram) provides a connectionless service. The packet is sent in the form of an independent packet, does not provide an error guarantee, the data may be lost or repeated, and the sequence of reception is disordered. Network file system NFS uses a datagram socket. 3. Original socket (socket_raw) This interface allows you to access a lower hierarchical protocol, such as IP, ICMP. Commonly used to check new protocols implementation or access new devices configured in existing services. Basic socket call Create a socket - Socket (); bind native port - Bind (); establish connection --connect (), accept (); listening port --Listen (); Data Transmission - -send (), RECV (); input / output multiplex - SELECT (); closing socket --closeSocket (); no language, and socket deduct this set of calls just in format There is a little difference. I only use C and Perl, plus anything that is not related to Perl here, then the main discussion of Perl's Socket programming: Create a socket: Socket (SOC_VARIABLE, DOMAIN_FLAG, CONNECTTYPE, NUM) # The corresponding C language call is SOCKID = SOCKET (AF, TYPE, Protocol) parameter meanings as follows: SOC_VARIABLE is a handle for establishing a set, equivalent to the SOCKID number in C; Domain_flag name tag, equivalent to AF-in C -address family, address family. The address family and domain are a concept, in fact, the usual domain. UNIX supported domain types with AF-UNIX; UNIX internal address AF-INET; TCP / IP address AF-NS; Xerox NS address AF-AppleTalk; Apple AppleTalk Address and DOS / Windows supported domain address family only AF-INET. So most of the Socket programming is only available. ConnectType (TYPE in C) is the three Socket types mentioned earlier. NUM is equivalent to the protocol of Protocol in C, I understand this is the protocol number, used to specify the protocol of the Socket request, this parameter does not necessarily work, the current two parameters can determine the protocol to be zero. So, a complete PERL creation Socket is as follows Socket (THESCK, AF-INET, SOCKET_STREAM, GETPROTOCOLBYNAME ('TCP')); #C Language: Int Sockid; # sockid = socket (AF-INET, SOCKET_STREAM, 0); Two steps: bind () - Bind to the local address. The first step Socket () call just specifies the protocol element of the relevant five-way group. Other four dollars need other calls to supplement. Socket creation can consider a name space (address family), but he is not named. Bind () binds the socket address with the socket created with this unit, the name of the socket, which is intended to the socket (handle) to specify local semi-associated. According to standard Socket (the so-called "Standard Interface" in the UNIX World is "the" C "program interface" does not distinguish between "nothing difference), the socket address is a data structure describing the Socket address.

The structure of the TCP / IP protocol address (AF_INET) is: struct sockaddr_in {short sin_family; // AF_INET U_SHORT SIN_PORT; / / 16-bit port number, network byte order struct in_addr sin_addr; // 32 bits IP address, network word Sequencing Char sin_zero [8]; // Reserved} Other structures There are SOCKADDR_NS, SOCKADDR_UN, etc., is used in other protocol addresses. Basically we can't use it. So a standard bound is: bind (socket sockid, structure * localadddr_name, int addrlen); // sockid is a unnamed socket number // localaddr_name is a pointer to the SockAddr_in structure for named Sockid // Addrlen Is the byte length of localaddr_name Use Perl's BIND () to call inet_aton ('localhost'); or function inaddr_any get the IP address string, then call $ localaddr_port = sockaddr_in ($ port, inet_aton ('localhost')); # $ port is the port number or $ localaddr_port = sockaddr_in ($ port, inaddr_any); get the address of TCP / IP, last bind (Server, $ localaddr_port); complete binding! This is the benefits of Perl here without specifying the byte length of $ localaddr_port. These two system calls are used to complete a full correlation, where connect () is used to establish a connection. Accept () enables the server to wait for the actual connection from a client process. Its call format is as follows (socket sockid, struct sockaddr * destaddr, int address); // sockid is a native Socket number to establish a connection // DestadDR is a pointer to the other Socket address (SBO address) structure // addrlen is the other party perl language connect socket address length () call format: connect (SOC_VARIABLE, NAME_VARIABLE) follows a specific call $ remoteaddr_port = sockaddr_in ($ port, inet_aton ( 'abc.efg.com')); connect (CLIENT, $ remoteaddr_port ); # Semi-related triplet (protocol, remote address, remote port number). It can be found that connect () and bind () calls are as follows, just Server is replaced with client, local replacement, true, their truth is the same, the role is complementary. They have established half-related servers and clients. At this time, you will be able to build the full correlation of a complete network process to communicate! (In fact, standard connect () can also be used for unconnected Socket calls. But this usage is more left, often got people, so I don't say it) standard accept () call: socket news = accept (socket sockid, Struct SockAddr * ClientDr, int addlen // sockid, local socket number // clientaddr, pointing to the pointer to the client's Socket structure. Its initial value is empty // AddLen, the character length of the customer socket structure, its initial value of 0 // news, accept () return value, for a new Socket number, can be used for server // processing concurrency request .

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

New Post(0)