Network programming is a mysterious and complex art, but of course it is also very interesting. The Perl language provides a wealth of TCP/ip network functions, all of which are directly derived from the socket library functions of the C language. Since the socket library functions of the Perl language and the C language are the same in form and usage, so I can use the Perl language for Socket programming, and of course I can also use the C language for Socket programming.
The following is a list of related socket library functions in the Perl language:
Instructions for using function prototypes
accept(NEWSOCKET, GENERICSOCKET) accepts the requested socket Connection. If successful, return the network address in compressed form; otherwise return FALSE.
Example:
if(!$Connect=accept(NEW,HANDLE))
{
die"Connectionfailed:$!
";
}
bind (SOCKET, NAME) establishes the binding between NAME and SOCKET, where NAME should be the compressed address corresponding to the correct type of socket.
If successful, return true; otherwise return false.
When using sockets for network programming, this function is very useful Important, because it establishes the association between the socket handle and an address on the network.
Example:
bind(SH,$SocketAddress);
connect(SOCKET,NAME) attempts to connect with the accept() function that has been called and is waiting to establish a connection. Another process has a conversation.
If successful, return true; otherwise return false.NAME should be the compressed address corresponding to the correct type of SOCKET handle
Example:
connect(SOCK,$address)||die"Can'tconnectwithremotehost:$!
";
gethostbyaddr(ADDRESS,TYPE) converts the compressed network address into a name and address that is easier for people to read and understand.
When only the IP address of the host is known, you can use this function to query the host name and other network information. It returns a list containing the following information:
($name,$alias,$addrtype,$length,$address)
where, $name is the host name corresponding to the IP address, $alias is other aliases corresponding to $name, $addrtype is the type of network address, $length is the length of the address, and $address is a list of compressed IP addresses.
Example:
$PackedAddress=pack("C4",$IPAddr);
($name, $alias,$addrtype,$length,$address)
=gethostbyaddr($PackedAddress,2);
gethostbyname(NAME) is similar to the gethostbyaddr() function above, but here the host name is used as a parameter. The format of the returned information is completely Same.
Example:
$Host="stuff.com";
($name,$alias,$addrtype,$length,$address)
=gethostbyname($Host);
@IP=unpack("C4", $address[0]);
$HostIP=join(".",@IP);
The original code of the program to verify the email password
The following code works on 263.net and pop.netease under two operating systems. com's two POP3 servers were rigorously tested and proved to be successful.
First type:
Operating system: Windows98 Chinese version
WWW server: Apache1.3.9forWin
Perl interpreter: ActiveStateToolCorp's PerlforWin32, version5.005_03builtforMSWin32-x86-object
Second type:
Operating system: RedHatlinux6.1
WWW server: Apache1.3.6forLinux
Perl interpreter: version5.005_03builtfori386-linux
#!/usr/bin/perl
#test.pl
#Authorhomepage: http: //spot.126.com
usestrict;
useSocket;
my$pop3server="263.net";
my$port=110;
$|=1;
PRint"Content-type:text/html
";
print"POP3
";
print"
";
my($a,$name,$aliases,$proto,$type,$len,$thataddr,$thisaddr,$i);
my$AF_INET=2;
my$SOCK_STREAM=1;
my$sockaddr="Sna4x8";
($name,$aliases,$proto)=getprotobyname("tcp");
($name,$aliases, $port)=getservbyname($port,"tcp")
unless$port=~/^d $/;;
($name,$aliases,$type,$len,$thataddr)=gethostbyname($pop3server);
my$this=pack($sockaddr,$AF_INET,12345,$thisaddr);
my$that=pack($sockaddr,$AF_INET,$port,$thataddr);
my$mysocket=socket(S, $AF_INET,$SOCK_STREAM,$proto);
if($mysocket)
{
}
else
{
print "Cannot 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="USERzhangsan";
$BUFFER.=chr(13);
$BUFFER.=chr(10);
my$SENVAL=send( S,$BUFFER,0);
if($SENVAL)
{
}
else
{
print "Sending 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"No such account!
";
exit(0);
}
$BUFFER="PASS12345678 ";
$BUFFER.=chr(13);
$BUFFER.=chr(10);
my$SENVAL=send(S,$BUFFER,0);
if($SENVAL)
{
}
else
{
print"Sending 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 "Wrong password!";
exit(0);
}
print The password is correct!
The above is the content of Socket programming using Perl language. For more related articles, please pay attention to the PHP Chinese website (www .php.cn)!