perl中网络编程的基本架构
套接字是信息的一个端口,创建一个套接字,我们需要给系统提供最少三个信息
1.套接字域:定义了网络协议族及套接字的寻址方案
AF_INET:Internet协议
AF_UNIX:单主机内部协议
2.套接字类型
3.一些套接字协议
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
先打包,再联合
$packer_address = inet_aton($dotted_quad);
相对于 函数 inet_ntoa
$socket_addr = sockaddr_in($port,$address);
($port,$address) = sockaddr_in($socket_addr);//在不同上下文环境中
直接使用
$socket_addr = pack_sockaddr_in($port,$address);
($port,$address) = unpack_sockaddr_in($socket_addr);
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
一个简单的服务器发起的访问
$socket_addr = pack_sockaddr_in($port,$address);
socket(SOCK,PF_INET,SOCK_STREAM,IPPROTO_TCP);
connect(SOCK,$socket_addr); #套接字与网址信息链接
print <SOCK>;
shutdown(SOCK,$how)
$who:0,关闭读;1,关闭写,2,完全关闭
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
一些基础函数
($name,$aliases,$type,$len,$packed_addr) = gethostbyname($name);
$packed_addr = gethostbyname($name);
($name,$aliases,$type,$len,$packed_addr) = gethostbyaddr($packed_addr,$family);
$name = gethostbyaddr($packed_addr,$fimaly)
$number = getprotobyname($protocol);
($name,$aliases,$numbr) = getprotobyname($protocol);
$name = getprotobynumber($protocol_number);
($name,$aliases,$number) = getprotobynumber($protocol_number);
$port = getservbyname($service,$protocol);
($name,$aliases,$port,$protocol) = getservbyname($service,$protocol);
$name = getservbyport($port,$protocol);
($name,$aliases,$port,$protocol) = getservbyport($port,$protocol);
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
套接字,TCP服务器端
先是服务器自身的设定
socket(SOCK,AF_INET,SOCK_STREAM,$protocol);
setsockopt(SOCK,SOL_SOCKET,SO_REUSEADDR,1);
bind(SOCK,$my_packed_addr);
listen(SOCK,SOMAXCONN);
对客户端的的链接
$remote_addr = accept(SECCION,SOCK);//链接成功远程压缩地址,作为函数的分那会结果
使用SECCION和对等端点通信
($port,$his_addr) = sockaddr_in($remoet_addr);
$my_pached_addr = getsockname(SOCK);
$remote_packede_addr = getpeername(SOCK);
调整套接字选项
$value = getsockopt(SOCK,$level,$option_name);
$boolean = setsockopt(SOCK,$level,$option_name,$option_value);
perl内建与套接字有关的函数
$bytes = send(SOCK,$data,$flags[,$destination]);
$address = recv(SOCK,$buffer,$length,$flags);
$boolean = socketpair(SOCK_A,SOCK_B,$domain,$type,$protocol);
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IO::Socket API
my $socket = IO::Socket::INET->new("$host:daytime");
my $time = $socket->getline;
$socket->print("这是写进$socket");
------
$connceted_socket = $listen_socket->accept();
($connect_socket,$remote_addr) = $listen_socket->accept();
例子:
$/ = CRLF;
my $sock = IO::Socket::INET->new(Listen =>20,LocalPort =>$port,TImeout=>60*60,Reuse=>1);
链接
$session = $sock->accept;
$peer = gethostbyaddr($session->peeraddr,AF_INET);
------------------------------------------------------------------------------
第二部分:为公共服务开发客户
FTP和Telnet
--------
my $ftp = Net::FTP->new(HOST);
$ftp->login('anonymous');
$ftp->cwd(DIR);
$ftp->get(FILE);
$ftp->quit;
等待补充....
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
SMTP:发送邮件
$smtp = Net::SMTP->new('mail.cshl.org',TImeout=>60);
$banner = $smtp->banner();
$domain = $smtp->domain();获取欢迎标题和识别该服务器的域名
首先建立交换
$success = $smtp->mail($from_address);
$suce = $smtp->recipient($to_address);
$smtp->data($text);
----
$smtp->datasend(@data);
$smtp->dataend();
----
$smtp->reset 重置服务,既取消先前的操作
-----
检查接受放地址是否有效
$smtp->verify($address);
$smtp->expand($address);
-----
$smrp->quit;
MailTools模块
$head = Mail::Header->new;
$head->add(From => '');
$head->add(To => '');
$head->add(Cc => '');
$head->add(Cc => '');
$head->add(Subject=> '');
$mail = Mail::Interent->new(Header = >$head,Body = >$body,Modify =>1);
print $mail->send('sendmail');使用内置的软件发送
--------------
Mail::Header