网络编程是一门神秘且复杂的艺术,当然也十分有趣。Perl语言提供了丰富的TCP/IP网络函数,所有这些函数都
直接来源于C语言的socket库函数.
由于Perl语言和C语言的socket库函数在型式和使用方法上都是一样的,因此会使用Perl语言进行Socket编程, 当
然也就会使用C语言进行Socket编程.
下面是Perl语言中有关的socket库函数列表:
函数原型 使用说明
accept(NEWSOCKET,GENERICSOCKET) 接受请求的socket连接.如果成功,则返回压缩形式的网络地址;否则返回FALSE.
范例:
if (!$Connect = accept(NEW,HANDLE))
{
die "Connection
failed: $!
";
}
bind(SOCKET,NAME)
建立NAME与SOCKET的绑定,其中NAME应该是对应socket正确类型的压缩地址.
如果成功,则返回真;否则返回假.
在使用socket进行网络编程时,这一函数十分重要,因为它建立了socket句柄与网络上某个地址的关联.
范例:
bind(SH,$SocketAddress);
connect(SOCKET,NAME)
试图与已经调用了accept()函数并等待建立连接的另外一个进程进行对话.
如果成功,则返回真;否则返回假.NAME应该是对应SOCKET句柄正确类型的压缩地址
范例:
connect(SOCK,$address) || die "Can’t connect with remote host: $!
";
gethostbyaddr(ADDRESS,TYPE) 将压缩形式的网络地址转换为更易于人阅读理解的名字与地址.
当只知道主机的IP地址时,可以使用本函数查询主机名及其他网络信息.它返回一个列表,包含如下信息:
($name, $alias,
$addrtype, $length, $address)
其中, $name是与IP地址对应的主机名, $alias是对应$name的其他别名,
$addrtype是网络地址的类型, $length是
地址的长度, 而$address则是压缩形式IP地址的列表.
范例:
$PackedAddress = pack("C4", $IPAddr);
($name, $alias, $addrtype,
$length, $address)
=gethostbyaddr($PackedAddress,2);
gethostbyname(NAME)
与上面的gethostbyaddr()函数类似,不过在这里主机名是作为参数.返回的信息格式完全相同.
范例:
$Host =
"stuff.com";
($name, $alias, $addrtype, $length, $address)
=gethostbyname($Host);
@IP = unpack("C4",$address[0]);
$HostIP =
join(".", @IP);
验证邮箱密码的程序的原码
下面的代码在两种操作系统下对263.net和pop.netease.com两个POP3服务器严格测试, 证明是成功的.
第一种:
操作系统:Windows 98 中文版
WWW服务器:Apache 1.3.9 for Win
Perl 解释器:ActiveState
Tool Corp的Perl for Win32 , version 5.005_03 built for MSWin32-x86-object
第二种:
操作系统:Red Hat Linux 6.1
WWW服务器:Apache 1.3.6 for Linux
Perl
解释器: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,$type,$len,$thataddr,$thisaddr,$i);
my $AF_INET = 2;
my $SOCK_STREAM = 1;
my $sockaddr = "S n a4 x8";
($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 "不能打开socket: $!";
exit(0);
}
my $mybind = bind(S,
$this);
if ($mybind)
{
}
else
{
print "无法绑定!: $!";
exit(0);
}
my $myconnect = connect(S,$that);
if ($myconnect)
{
}
else
{
print "连接错误: $!";
exit(0);
}
my
$BUF = "";
my $SenderIP = recv(S, $BUF, 596,0);
if ($SenderIP)
{
}
else
{
print "接收错误: $!";
exit(0);
}
if
(substr($BUF,0,3) eq "+OK")
{
}
else
{
print "POP3服务器出错!
";
exit(0);
}
my $BUFFER = "USER zhangsan";
$BUFFER
.= chr(13);
$BUFFER .= chr(10);
my $SENVAL = send(S, $BUFFER,0);
if ($SENVAL)
{
}
else
{
print "发送错误: $!";
exit(0);
}
my $BUF = "";
my $SenderIP = recv(S, $BUF, 4096,0);
if
($SenderIP)
{
}
else
{
print "接收错误: $!";
exit(0);
}
if (substr($BUF,0,3) eq "+OK")
{
}
else
{
print
"无此帐号!
";
exit(0);
}
$BUFFER = "PASS 12345678";
$BUFFER .= chr(13);
$BUFFER .= chr(10);
my $SENVAL = send(S,
$BUFFER,0);
if ($SENVAL)
{
}
else
{
print "发送错误: $!";
exit(0);
}
$BUF = "";
my $SenderIP = recv(S, $BUF, 196, 0);
if ($SenderIP)
{
}
else
{
print "接收错误: $!";
exit(0);
}
if (substr($BUF,0,3) eq "+OK")
{
}
else
{
print "密码错误!";
exit(0);
}
直接来源于C语言的socket库函数.
由于Perl语言和C语言的socket库函数在型式和使用方法上都是一样的,因此会使用Perl语言进行Socket编程, 当
然也就会使用C语言进行Socket编程.
下面是Perl语言中有关的socket库函数列表:
函数原型 使用说明
accept(NEWSOCKET,GENERICSOCKET) 接受请求的socket连接.如果成功,则返回压缩形式的网络地址;否则返回FALSE.
范例:
if (!$Connect = accept(NEW,HANDLE))
{
die "Connection
failed: $!
";
}
bind(SOCKET,NAME)
建立NAME与SOCKET的绑定,其中NAME应该是对应socket正确类型的压缩地址.
如果成功,则返回真;否则返回假.
在使用socket进行网络编程时,这一函数十分重要,因为它建立了socket句柄与网络上某个地址的关联.
范例:
bind(SH,$SocketAddress);
connect(SOCKET,NAME)
试图与已经调用了accept()函数并等待建立连接的另外一个进程进行对话.
如果成功,则返回真;否则返回假.NAME应该是对应SOCKET句柄正确类型的压缩地址
范例:
connect(SOCK,$address) || die "Can’t connect with remote host: $!
";
gethostbyaddr(ADDRESS,TYPE) 将压缩形式的网络地址转换为更易于人阅读理解的名字与地址.
当只知道主机的IP地址时,可以使用本函数查询主机名及其他网络信息.它返回一个列表,包含如下信息:
($name, $alias,
$addrtype, $length, $address)
其中, $name是与IP地址对应的主机名, $alias是对应$name的其他别名,
$addrtype是网络地址的类型, $length是
地址的长度, 而$address则是压缩形式IP地址的列表.
范例:
$PackedAddress = pack("C4", $IPAddr);
($name, $alias, $addrtype,
$length, $address)
=gethostbyaddr($PackedAddress,2);
gethostbyname(NAME)
与上面的gethostbyaddr()函数类似,不过在这里主机名是作为参数.返回的信息格式完全相同.
范例:
$Host =
"stuff.com";
($name, $alias, $addrtype, $length, $address)
=gethostbyname($Host);
@IP = unpack("C4",$address[0]);
$HostIP =
join(".", @IP);
验证邮箱密码的程序的原码
下面的代码在两种操作系统下对263.net和pop.netease.com两个POP3服务器严格测试, 证明是成功的.
第一种:
操作系统:Windows 98 中文版
WWW服务器:Apache 1.3.9 for Win
Perl 解释器:ActiveState
Tool Corp的Perl for Win32 , version 5.005_03 built for MSWin32-x86-object
第二种:
操作系统:Red Hat Linux 6.1
WWW服务器:Apache 1.3.6 for Linux
Perl
解释器: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;
"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 = "S n a4 x8";
($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 "不能打开socket: $!";
exit(0);
}
my $mybind = bind(S,
$this);
if ($mybind)
{
}
else
{
print "无法绑定!: $!";
exit(0);
}
my $myconnect = connect(S,$that);
if ($myconnect)
{
}
else
{
print "连接错误: $!";
exit(0);
}
my
$BUF = "";
my $SenderIP = recv(S, $BUF, 596,0);
if ($SenderIP)
{
}
else
{
print "接收错误: $!";
exit(0);
}
if
(substr($BUF,0,3) eq "+OK")
{
}
else
{
print "POP3服务器出错!
";
exit(0);
}
my $BUFFER = "USER zhangsan";
$BUFFER
.= chr(13);
$BUFFER .= chr(10);
my $SENVAL = send(S, $BUFFER,0);
if ($SENVAL)
{
}
else
{
print "发送错误: $!";
exit(0);
}
my $BUF = "";
my $SenderIP = recv(S, $BUF, 4096,0);
if
($SenderIP)
{
}
else
{
print "接收错误: $!";
exit(0);
}
if (substr($BUF,0,3) eq "+OK")
{
}
else
{
"无此帐号!
";
exit(0);
}
$BUFFER = "PASS 12345678";
$BUFFER .= chr(13);
$BUFFER .= chr(10);
my $SENVAL = send(S,
$BUFFER,0);
if ($SENVAL)
{
}
else
{
print "发送错误: $!";
exit(0);
}
$BUF = "";
my $SenderIP = recv(S, $BUF, 196, 0);
if ($SenderIP)
{
}
else
{
print "接收错误: $!";
exit(0);
}
if (substr($BUF,0,3) eq "+OK")
{
}
else
{
print "密码错误!";
exit(0);
}