Use perl language to program Socket programming

zhaozj2021-02-08  271

Use perl language to program Socket programming

Published: March 28, 2001 00:00

Network programming is a mysterious and complicated art, of course very interesting.

Perl language provides a rich TCP / IP network function, all of these functions are directly from the C language Socket library function. Since the Perl language and C language Socket library functions are the same in terms of use, it will be used Perl language is Socket programming, of course, Socket programming is also used in the C language. Below is the list of Socket library functions related to Perl language: Function prototype instructions Accept (newsocket, genericsocket) Accept the Socket connection. If successful, return Compressed form of network address; otherwise returning false. Example: if (! $ Connect = accept (new, handle)) {Die "Connection Failed: $!";} Bind (socket, name) Established Name and Socket Binds, Where Name should be a corresponding socket correct type of compressed address. If successful, return true; otherwise return to the network programming, this function is very important because it establishes a socket handle and an address on the network. Association. Example: Bind (SH, $ SocketAddress); Connect (Socket, Name) Attempts to talk to another process that has been called an Accept () function and waits for the establishment of a connection. If success, return true; otherwise return .Name It should be the correct type of the corresponding socket handle: Connect (Sock, $ address) || DIE "Can't Connect With Remote Host: $!"; Gethostbyaddr (address, type) converts the network address of the compressed form to more Easy to read the name and address of understanding. When you only know the host's IP address, you can use this function to query host names and other network information. It returns a list, which includes the following information: ($ Name, $ Alias, $ addrtype, $ Length, $ address, $ name is the host name corresponding to the IP address, $ alias is the other alias corresponding $ name, $ addRTYPE is the type of network address, $ length is the length of the address, and $ address is compressed form List of IP addresses. Example: $ Packedaddress = PACK ("C4", $ Ipaddr); ($ Name, $ Alias, $ AddRTYPE, $ Length, $ Address = getho Stbyaddr ($ Packedaddress, 2); gethostByname (Name) is similar to the getHostByAddr () function above, but the host name here is parameters. The return information format is exactly the same. Example: $ host = "stuff.com"; ($ Name, $ ALIAS, $ AddRTYPE, $ Length, $ address = gethostbyname ($ host); @ip = unpack ("c4", $ address [0]); $ hostip = join (".", @ip); Verify the original code of the program of the mailbox password The code below the two POP3 servers under the two operating systems, the two POP3 servers are strictly tested, prove is successful. The first: Operating system: Windows 98 Chinese version WWW server: Apache 1.3.9 for Win Perl Interpreter: ActiveState Tool Corp's Perl for Win32, Version 5.005_03 Built for MSWIN32-x86-Object 2: Operating System: Red Hat Linux 6.1 WWW Server:

Apache 1.3.6 for Linux Perl Interpreter: Version 5.005_03 Built for i386-linux #! / Usr / bin / perl # Test.pl #author homepage: http://spot.126.com use strict; use socket; My $ pop3server = "263.net"; My $ port = 110; $ | = 1; Print "Content-type: text / html"; Print "POP3"; Print "; My ($ A, $ Name, $ AliaSES $ PROTO, $ TEPE, $ LEN, $ THATDR, $ THISADDR, $ I); MY $ AF_INET = 2; My $ SOCK_STREAM = 1; MY $ SOCKADDR = "S N A4 X8"; ($ Name, $ AliaS, $ proto) = GetProtobyname ($ Name, $ AliaS, $ port) = GetServbyName ($ Port, "TCP") Unless $ Port = ~ / ^ D $ / ;; ($ Name, $ Aliases, $ TYPE, $ LEN, $ THATDR) = gethostbyname ($ POP3SERVER); My $ this = Pack ($ SOCKADDR, $ AF_INET, 12345, $ THISADDR); My $ THATDD ($ SOCKADDR, $ AF_INET, $ Port, $ Thataddr); My $ mysocket = Socket (S, $ AF_INET, $ SOCK_STREAM, $ Proto); if ($ mysocket) {} else {print "Can't open Socket: $!"; exit (0);} my $ mybind = Bind (S, $ this); if ($ mybind) {} ​​else {print "Unable to bind!: $!"; exit (0);} my $ myconnect = connect (s, $ there); if ($ MyConnect) ) {} else {print "connection error: $!"; exit (0);} my $ bu F = ""; MY $ senderip = RECV (S, $ BUF, 596, 0); if ($ senderip) {} else {print "Receive error: $!"; Exit (0);} if (Substr BUF, 0, 3) EQ " OK") {} else {print "POP3 server error!"; Exit (0);} my $ buffer = "User zhangsan"; $ buffer. = Chr (13); $ buffer = CHR (10); My $ SENVAL = Send (S, $ Buffer, 0); if ($ SENVAL) {} Else {Print "Send Error: $!"; exit (0);} my $ buf = " "; MY $ senderip = RECV (S, $ BUF, 4096, 0); if ($ senderip) {} else {print" Receive error: $! "; exit (0);} if (Substr ($ buf, 0 , 3) EQ " OK"

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

New Post(0)