To use the Perl Socket API, you must first load the socket module. Use SOCKET; ========================================================== ==================================== Socket (file handle, af_inet, data type, protocol type ); # Create a socket You can find a word for the file handle. Af_inet is a domain type or pf_inet. Data Types: sock_stream and sock_dgram. Protocol type, can be replaced by Protocol Number, EGP---8, HMP---20, ICMP---1, Raw---255, RDP---27, RVD---66, TCP---6, UDP---17, XNS-IDP---22, Other --- 22, all---0; you can also use the getprotobyname () function for this parameter. Example: socket (sock, af_inet, sock_stream, getprotobyname ('tcp ')); The language handle is sock, and the socket is transmitted in TCP mode. Socket (SS, af_inet, sock_dgram, 17 ); The socket is transmitted in UDP mode. ========================================================== ==================================== Connect (file handle, addr_in struct ); # Connecting hosts ----------------------------------------------- Sockaddr_in struct: $ Address = inet_aton (Address ); $ Port = port number; $ Result = sockaddr_in ($ port, $ address ); # The above $ result is the sockaddr_in struct, instance: $ Address = inet_aton (127.0.0.1 ); $ Port = 80; $ Result = sockaddr_in ($ port, $ address ); ----------------------------------------------- Example: connect (sock, $ result ); ========================================================== ==================================== BIND (socket, sockaddr_in struct ); # Bind the server host address and port (for the server) Example: BIND (sock, $ result ); ========================================================== ==================================== Listen (socket, maximum number of waiting queues ); # Set the port status to listener (for the server) Example: Listen (sock, 10 ); ========================================================== ==================================== Accept (Remote socket, server listening socket) # Receive remote data requests and establish a connection (for the server) Example: accept (Session, sock ); ========================================================== ==================================== Close (file handle ); Or Close the file handle; # Disable socket Example: Close (sock ); Close SS; ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●● ●●●●●●●●●● Let's talk about the TCP network activity sequence: ========================================================== ==================================== Client ): Establish Socket socket ()-> connect to the target host connect ()-> enable autoflush mode autoflush ()-> I/O operations-> close socket close () Server ): Establish Socket socket ()-> bind the server address and port BIND ()-> set the listening status listen ()-> Accept Remote socket accept ()-> Open autoflush mode autoflush ()-> I/O operations-> close socket close () ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●● ●●●●●●●●●● ◎ ◎ Perl Socket API programming example: Note: Only print () and <> symbols are used for I/O operations. ========================================================== ==================================== #! Usr/bin/perl # Client Use IO: handle; # suspend IO: handle Use SOCKET; # Call socket $ Port = 80; # port 80 connecting to the remote host $ Host = 'localhost'; # Use the loopback address $ Packhost = inet_aton ($ host); # compressing IP addresses $ Address = sockaddr_in ($ port, $ packhost); # press it into the sockaddr_in format Socket (client, af_inet, sock_stream, 6); # The socket is client and the TCP protocol is used. Connect (client, $ address); # connect Client-> autoflush (1); # enable autoflush Mode $ Msg_in = # Input Print "in: $ msg_in/N"; # output Close client; # Close socket Exit 1; # exit the program ========================================================== ==================================== #! Usr/bin/perl # Server Use IO: handle; # suspend IO: handle Use SOCKET; # Call socket $ Port = 80; # The bound server host port is 80. $ Address = sockaddr_in ($ port, inaddr_any); # press it into the sockaddr_in format and use the inaddr_any wildcard Socket (server, af_inet, sock_stream, getprotobyname ('tcp '); # The socket is server and the TCP protocol is used. BIND (server, $ address); # bind Listen (server, 10); # Set the listening status While (1) {# enter the I/O Switching loop body Next unless (accept (client, server )); Client-> autoflush (1 ); Print client "what do you want? /N "; Close client ;} Close server; # Close socket Exit 1; # exit the program ========================================================== ==================================== Instance annotation: 1) there is a line of 'sock-> autoflush (1); 'in the tcp client and server code. Normally, the following I/O Code first enters the cache, But the above Code can be skipped and output directly. This code needs to be preloaded with IO: handle. 2) The value of the inaddr_any wildcard has been defined in the socket module, and its value is all network interfaces (including the loopback address, Broadcast address, multicast address, etc ). ◎ ◎ This is a bit painful. Write it here. Next we will introduce send () and Recv () ......: P [Diary] 'send () 'and 'recv' [perl_sock 2] Writer: demonalex Email: demonalex_at_dark2s.org Some content of the link, the data exchange part of the last two c/s programs uses Perl I/O, Now we will introduce the 'original' network data exchange functions allocated to sockets by Perl: Send () and Recv (). (These two functions play a major role in UDP, but for TCP, they can only be said to be equal to syswrite () and sysread ().) ========================================================== ==================================== Byte variable = Send (socket, transfer data variable, flag parameter ); The send () function is used to send data in a socket process. The value returned by send () is the variable that stores the sent byte size value. The transmitted data variable is the content of the transmitted data; The flag parameter is 0 (the default value is enough ). Example: $ bytes = Send (sock, $ data, 0 ); ========================================================== ==================================== Address variable = Recv (socket, the variable stored in the received data, the length of the received data, flag parameter ); The Recv () function is used to receive data in a socket process. Recv () returns the address variable of the remote host; the second parameter is the variable stored in the received data; the third parameter The parameter is the length of the received data. The flag parameter can also be set to 0 by default. Example: $ address = Recv (sock, $ buffer, $ length, 0 ); ========================================================== ==================================== Lab 1 #! Usr/bin/perl # Client Use IO: handle; Use SOCKET; $ Port = 80; $ Host = 'localhost '; $ Packhost = inet_aton ($ host ); $ Address = sockaddr_in ($ port, $ packhost ); Socket (client, af_inet, sock_stream, 6 ); Connect (client, $ address ); Client-> autoflush (1 ); Recv (client, $ msg_in, length ($ msg_in), 0 ); Print "in: $ msg_in/N "; Close client; Exit 1; ========================================================== ==================================== #! Usr/bin/perl # Server Use IO: handle; Use SOCKET; $ Port = 80; $ Host = 'localhost '; $ Packhost = inet_aton ($ host ); $ Address = sockaddr_in ($ port, $ packhost ); Socket (server, af_inet, sock_stream, getprotobyname ('tcp ')); BIND (server, $ address ); Listen (server, 10 ); While (1 ){ Next unless (accept (client, server )); Client-> autoflush (1 ); $ Msg_out = "what do you want? /N "; Send (client, $ msg_out, 0 ); Close client ;} Close server; Exit 1; [Journal] UDP of Perl socket [perl_sock 3] Writer: demonalex Email: demonalex_at_dark2s.org Continue with the send () and Recv () mentioned above. This article will talk about their application in UDP socket and Use the Perl Socket API to call UDP. Let's take a look at the send () and Recv () Applications in UDP: ========================================================== ======================================== Byte variable = Send (socket, transfer data variable, flag parameter, sending Address ); The send () function is used to send data in a socket process. The value returned by send () is the variable that stores the sent byte size value. The transmitted data variable is the content of the transmitted data; The flag parameter is 0 (the default value is enough); send () adds the last parameter in UDP, 'sending address ', The data format of this address is sockaddr_in, indicating that the second parameter 'transfer data variable 'is sent to this address. Example: $ bytes = Send (sock, $ data, 0, $ address ); In the preceding example, $ address is in sockaddr_in format. ========================================================== ======================================== Address variable = Recv (socket, the variable stored in the received data, the length of the received data, flag parameter ); The Recv () function is used to receive data in a socket process. Recv () returns the address variable of the remote host; the second parameter is the variable stored in the received data; the third parameter The parameter is the length of the received data. The flag parameter can also be set to 0 by default. Example: $ address = Recv (sock, $ buffer, $ length, 0 ); ========================================================== ======================================== From the upstairs explanation, we can know that in UDP call, send () has one more parameter than in TCP call, while Recv () and The usage is the same. ------------------------------------------------------------------------ UDP activity sequence: Client ): Establish Socket socket ()-> send data send ()-> Accept data Recv ()-> close socket close () Server ): Establish Socket socket ()-> Bind Address ()-> Accept data Recv ()-> send data send ()-> close socket close () ------------------------------------------------------------------------ From the upstairs process, it is not difficult to find the difference between the client and the server in UDP: 1) the Server adds one more socket BIND () program to enable the client to identify the network address and port of the server; 2) reverse the order of send () and Recv () Steps Yes. 〓〓 Finally, let's take a look at the example and ponder over: When there are too many threads, there are too many threads, too many threads. #! Use/bin/perl-W # UDP client Use SOCKET; # import the socket Library $ Host = $ argv [0]; # The first parameter is the host variable. $ Port = $ argv [1]; # The second parameter is the port variable. $ Packhost = inet_aton ($ host); # compress the host address $ Address = sockaddr_in ($ port, $ packhost); # Press sockaddr_in Mode Socket (client, af_inet, sock_dgram, 17); # create a UDP socket Send (client, "Hi, body! /N ", 0, $ address); # Send a string variable to the socket Recv (client, $ buff,); # receive data Print "$ buff/N"; # input the received data to stdout. Close client; # Close socket Exit 1; # exit the program When there are too many threads, there are too many threads, too many threads. #! Use/bin/perl-W # UDP Server Use SOCKET; # import the socket Library $ Localhost = sockaddr_in (4000, inaddr_any); # press the sockaddr_in mode and use the global local compression address inaddr_any reserved word. Socket (server, af_inet, sock_dgram, 17); # create a UDP socket BIND (server, $ localhost); # bind a socket While (1) {# enter the server loop body Next unless $ client = Recv (server, $ buff, 0); # press the data into $ buff if the data is received, and keep the remote address at $ Client Chop ($ buff); # Minus the last input symbol of $ buff Print "$ buff/N"; # input stdout in the $ buff variable Send (server, "$ buff/N", 0, $ client); # Send $ buff to the client } Close server; # Close socket Exit 1; # exit the program When there are too many threads, there are too many threads, too many threads. [Diary] summary [perl_sock 4] Writer: demonalex Email: demonalex_at_dark2s.org This is the summary of the first three articles. The TCP server I/O struct: ----------------------------------------------- While (1 ){ Next unless (accept (client, server )); Client-> autoflush (1 ); Print client "what do you want? /N "; Close client ;} ----------------------------------------------- UDP server I/O struct: ----------------------------------------------- While (1 ){ Next unless $ client = Recv (server, $ buff, 100,0 ); Chop ($ buff ); Print "$ buff/N "; Send (server, "$ buff/N", 0, $ client ); } ----------------------------------------------- From the above example, we can see that the server's I/O bodies are all cyclic bodies, and there are specific conditions for looping (Here we use an infinite loop while (1) to enable the server to constantly listen. Tcp I/O is characterized by generating a client socket in accept (). All I/O operations are In this socket, When I/O is complete, first close the client socket, and finally in the program To close the socket on the server. Udp I/O features Recv (), because the example in the article about UDP is Return the input data to the client program. The next step on the server is to call send, The last parameter in send () is the address in the form of sockaddr_in, and cannot be called in UDP. Accept (), so you cannot know where the other object is. You can only return the value through Recv. Recv () The return value is the sockaddr_in address of the data sent by the other party. |