The parameter description is as follows: Soc_variable is used to create a socket handle, which is equivalent to the sockid in C; domain_flag is called a domain tag, and in C It is equivalent to af -- address family, the address family. The address family and domain are a concept, which is actually a domain. UNIX supports the following domain types: AF-UNIX; UNIX internal address AF-INET; TCP/IP address AF-NS; Xerox NS address AF-APPLETALK; Apple's appletalk address The domain address family supported by DOS/Windows only has AF-INET, so most socket programming only uses it. Connecttype (type in C) is the three socket types mentioned above. Num is equivalent to the Protocol in C. once you see it, you can see that this is the Protocol number used to specify the Protocol that the socket request expects. This parameter does not necessarily work, currently, the two parameters can be set to zero. Therefore, the establishment of a complete Perl socket is as follows: Socket (thesck, AF-INET, socket_stream, getprotocolbyname ('tcp ')); # C language: int sockid; # Sockid = socket (AF-INET, socket_stream, 0 );Step 2: BIND () -- bind to the local address. In the first step, the socket () call only specifies the Protocol element of the relevant quintuple. Additional calls are required for the other four elements. The creation of socket can be considered as creating a namespace (address family), but it is not named. BIND () binds the socket address to the socket handle (socket number in c) created on the local machine, that is, the name is assigned to the socket (handle) to specify the local semi-correlation. According to the standard socket (in the Unix world, the so-called "Standard Interface" is no different from the "C Programming Interface ), the socket address is a data structure that describes the socket address. The structure of the TCP/IP protocol address (af_inet) is as follows: Struct sockaddr_in { Short sin_family; // af_inet U_short sin_port; // 16-bit port number, network byte sequence Struct in_addr sin_addr; // 32-bit IP address, in bytes Char sin_zero [8]; // Reserved } Other structures include sockaddr_ns and sockaddr_un, which are used for other protocol addresses. Basically, we cannot use it. Therefore, a standard binding is: BIND (socket sockid, struct * localaddr_name, int addrlen ); // Sockid is the socket Number of An untitled socket. // Localaddr_name is the pointer to the sockaddr_in structure used to name sockid. // Addrlen is the byte length of localaddr_name When using Perl BIND (), you must first call inet_aton ('localhost'); or use the inaddr_any function to obtain the IP address string, and 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 TCP/IP address, and finally BIND (server, $ localaddr_port ); Complete binding! Here, you do not need to specify the length of $ localaddr_port in bytes, which is the benefit of Perl. These two system calls are used to complete a fully related establishment, and connect () is used to establish a connection. Accept () causes the server to wait for the actual connection from a client process. The call format is as follows: Connect (socket sockid, struct sockaddr * destaddr, int addrlen ); // Sockid is the local socket number to establish the connection // Destaddr is a pointer to the structure of the socket address (sink address) of the other party. // Addrlen is the socket address length of the other party
The CONNECT () call format in Perl is: Connect (soc_variable, name_variable) The specific call process is as follows: $ Remoteaddr_port = sockaddr_in ($ port, inet_aton ('abc .efg.com ')); Connect (client, $ remoteaddr_port); # semi-related triple (protocol, remote address, remote port number ). It can be found that the connect () and bind () calls are exactly the same, but the server is changed to the client, and the local is changed to remote. That's right, they have the same principle, and their functions are complementary. They have established semi-correlation between servers and clients. In this case, accept () is required to be mixed with the full correlation of a complete inter-network process communication! (In fact, standard connect () can also be used for connectionless socket calls. However, this method is relatively left-side, and people are often confused, so I won't talk about it) Standard accept () call: Socket newsock = accept (socket sockid, struct sockaddr * clientaddr, int addlen) // Sockid, the local socket Number of the server // Clientaddr, a pointer to the customer's socket structure. Its initial value is null. // Addlen: The Byte Length of the client socket structure. Its initial value is 0. // Newsock, the return value of accept (), is a new socket number, which can be used by the server // to process concurrent requests. The server fork is a sub-server process that uses this socket number to return // Answer the customer request received by accept () It can be seen that accept () is a connection-oriented server call. It also places the client's socket address and Its byte length in clientaddr and addlen to specify the sink address for other connectionless calls. However, the connectionless usage that is flexible but not principled does not exist in Perl. The usage of Perl deems that socket must be connection-oriented. Please refer to the accept () in Perl (): Accept (new_soc_variable, current_soc_variable ); You can see that accept () connects a client from the current socket handle to the new socket handle. The return value is the client address (sink address ). In fact, once a connection is established, the service provider does not need to know the sink address, as long as bit stream is instilled in the set. The advantage of this is that the Protocol is more transparent and easy to understand to applications. Before calling accept (), you should call listen (). Listen () is used to listen on the port and receive connections. If you do not call listen (), accept () cannot be connected back to the client from the current socket. Standard listen (): Listen (sockid, Quelen ); // Socket number. The server is willing to receive requests from it // Quelen, the length of the Request queue, and listen () Limit the number of queued requests
Perl listen (): Listen (soc_variable, num); # similar to the C language version
Soc_variable is the socket handle, and num is the length of the Request queue. So far, the quintuple of a connection is all in one. |