Network programming is a mysterious and complicated art, of course very interesting. Perl language provides a wealth of TCP / IP network functions, all of which are directly derived from the Socket library function of the C language.
Since the Perl language and C language SOCKET library functions are the same on a model and usage, Socket programming is permined using the Perl language, and of course, Socket programming is performed using the C language.
Below is a list of Socket library functions related to the Perl language:
Function prototype instructions
Accept (newsocket, genericsocket) accepts the requested Socket connection. If success, return the network address of the compressed form; otherwise return false.
example:
IF (! $ connect = accept (new, handle))
{
Die "Connection Failed: $!
"
}
Bind (socket, name) establishes NAME and socket bindings, where Name should be a corresponding socket correct type of compressed address.
If successful, return true; otherwise return the false.
This function is important when using the socket for network programming because it establishes the association between the Socket handle and an address on the network.
example:
Bind (SH, $ SocketAddress);
Connect (socket, name) attempts to dialogue with another process that has been called an Accept () function and waits for a connection.
If successful, return true; otherwise return to the false .Name should be the correct type of the corresponding socket handle correct type
example:
Connect (Sock, $ Address) || Die "Can't Connect with Remote Host: $!
"
GethostbyAddr (Address, Type) converts the network address of the compressed form into the name and address of the easiest read 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 that contains the following information:
($ Name, $ ALIAS, $ AddRTYPE, $ Length, $ Address
Among them, $ 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 a list of compressed form IP addresses.
example:
$ Packedaddress = PACK ("C4", $ ipaddr);
($ Name, $ ALIAS, $ AddRTYPE, $ Length, $ Address
= gethostbyaddr ($ packedaddress, 2);
GethostByName (Name) is similar to the getOstbyAddr () function above, but the host name here is as a parameter. 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 following code is strictly tested on 263.NET and POP.NETEASE.COM under two operating systems, proved to be 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
Second:
Operating system: Red Hat Linux 6.1www 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, $ PROTO, $ TYPE, $ LEN, $ THATDR, $ THISADDR, $ I);
MY $ AF_INET = 2;
MY $ SOCK_STREAM = 1;
MY $ SOCKADDR = "S N A4 X8";
($ Name, $ AliaS, $ Proto) = GetProtobyname ("TCP");
($ Name, $ aliases, $ port) = getServbyName ($ Port, "TCP")
Unless $ port = ~ / ^ D $ / ;;
($ Name, $ AliaS, $ TYPE, $ LEN, $ THATDR) = gethostbyname ($ POP3SERVER);
MY $ this = Pack ($ SOCKADDR, $ AF_INET, 12345, $ THISADDR);
MY $ THAT = Pack ($ SOCKADDR, $ AF_INET, $ Port, $ THATDR);
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, $ That);
IF ($ myConnect)
{
}
Else
{
PRINT "Connection error: $!";
exit (0);
}
MY $ BUF = "";
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")
{
}
Else
{
Print "Non to this account!
"
exit (0);
}
$ Buffer = "Pass 12345678";
$ Buffer. = CHR (13);
$ Buffer. = Chr (10);
MY $ SENVAL = Send (S, $ Buffer, 0);
IF ($ SENVAL)
{
}
Else
{
Print "Send Error: $!";
exit (0);
}
$ BUF = "";
MY $ Senderip = RECV (S, $ BUF, 196, 0);
IF ($ senderip)
{
}
Else
{
Print "Receive error: $!";
exit (0);
}
IF (Substr ($ BUF, 0, 3) EQ " OK")
{
}
Else
{
Print "Password error!";
exit (0);
}
The PRINT password is correct!