伺服器端:
#!/usr/bin/perl
use io::socket;
$sock = io::socket::inet->new(localaddr=>'127.0.0.1',
localport=>8080,
proto=>'tcp',
listen=>3,
reuse=>1);
$sock_2 = $sock->accept();
while($data=)
close $sock;
客戶端:
#!/usr/bin/perl
use io::socket;
$sock = io::socket::inet->new('127.0.0.1::8080');
$msg = "hello";
$sock->print($msg);
close $sock;
從伺服器獲取資訊範例:
#!/usr/bin/perl -w
# tcp_socket_cli.pl
use strict;
use socket;
my $addr = $argv[0] || '127.0.0.1';
my $port = $argv[1] || '3000';
my $dest = sockaddr_in($port, inet_aton($addr));
my $buf = undef;
socket(sock,pf_inet,sock_stream,6) or
die "can't create socket: $!";
connect(sock,$dest) or
die "can't connect: $!";
my $bs = sysread(sock, $buf, 2048);
# try to read 2048
print "received $bs bytes, content $buf\n";
# actually get $bs bytes
close sock;
#!/usr/bin/perl -w
# tcp_socket_cli.pl
use strict;
use socket;
my $addr = $argv[0] || '127.0.0.1';
my $port = $argv[1] || '3000';
my $dest = sockaddr_in($port, inet_aton($addr));
my $buf = undef;
socket(sock,pf_inet,sock_stream,6) or die "can't create socket: $!";
connect(sock,$dest) or die "can't connect: $!";
my $bs = sysread(sock, $buf, 2048); # try to read 2048
print "received $bs bytes, content $buf\n"; # actually get $bs bytes
close sock;
執行結果:
perl tcp_socket_cli.pl localhost 25
received 41 bytes, content 220 esmtp postfix - extmail 0.12-hzqbbc
tcp 服務端 socket模組, forking/accept模型
簡介:乙個多程序的tcp 伺服器,sample中實現了daytime的功能
#!/usr/bin/perl -w
# tcp_socket_dt_srv.pl
use strict;
use socket;
use io::handle;
use posix qw(wnohang);
my $port = $argv[0] || '3000';
my $proto = getprotobyname('tcp');
$sig = sub
};socket(sock, af_inet, sock_stream, getprotobyname('tcp'))
or die "socket() failed: $!";
setsockopt(sock,sol_socket,so_reuseaddr,1)
or die "can't set so_reusaddr: $!" ;
my $my_addr = sockaddr_in($port,inaddr_any);
bind(sock,$my_addr) or die "bind() failed: $!";
listen(sock,somaxconn) or die "listen() failed: $!";
warn "starting server on port $port...\n";
while (1) else
}close sock;
利用上述tcp_socket_cli.pl訪問該server的執行結果:
[hzqbbc@local misc]$ perl tcp_socket_dt_srv.pl
starting server on port 3000...
connection from [127.0.0.1,32888]
connection from [127.0.0.1,32888] finished
reaped child 13927
forking child 13927
tcp 客戶端 ,io::sockiet模組
簡介:同樣為客戶端,不過使用的是io::socket 物件導向模組
#!/usr/bin/perl -w
# tcp_iosocket_cli.pl
use strict;
use io::socket;
my $addr = $argv[0] || '127.0.0.1';
my $port = $argv[1] || '3000';
my $buf = undef;
my $sock = io::socket::inet->new(
peeraddr => $addr,
peerport => $port,
proto => 'tcp')
or die "can't connect: $!\n";
$buf = <$sock>;
my $bs = length($buf);
print "received $bs bytes, content $buf\n"; # actually get $bs bytes
close $sock;
tcp 服務端, io::socket模組, forking/accept模型
簡介:同樣的乙個daytime 伺服器,使用io::socket重寫。
#!/usr/bin/perl
# tcp_iosocket_dt_srv.pl
use strict;
use io::socket;
use posix qw(wnohang);
$sig = sub
};my $port = $argv[0] || '3000';
my $sock = io::socket::inet->new( listen => 20,
localport => $port,
timeout => 60*1,
reuse => 1)
or die "can't create listening socket: $!\n";
warn "starting server on port $port...\n";
while (1) else
}close $sock;
perl實現socket通訊
perl中可以直接呼叫linux下c庫中berkeley套接字內建介面,呼叫方式和c語言一致,只是換成perl的語法而已。偶就不說了,網上一籮筐資料。我使用perl中的io socket模組來開發,個人經驗使用perl語言開發的過程中能使用模組的盡量使用,乙個是可靠,二個是方便,最主要的是便於跨平台...
使用Perl編寫CGI程式(前後端)
cgi common gateway inte ce 是www技術中最重要的技術之一,有著不可替代的重要地位。cgi是外部應用程式 cgi程式 與web伺服器之間的介面標準,是在cgi程式和web伺服器之間傳遞資訊的過程。cgi規範允許web伺服器執行外部程式,並將它們的輸出傳送給web瀏覽器,cg...
Python 使用socket編寫TCP服務程式
使用socket編寫tcp服務程式 by 鄭瑞國 1 引用網路模組socket 2 建立乙個tcp網路套接字s 3 繫結本機ip位址和指定埠號 4 開始監聽 5 接受連線 6 接受資訊 import socket 1 引用網路模組socket s socket.socket socket.af in...