- 论坛徽章:
- 0
|
本帖最后由 zmj9152 于 2016-07-23 19:19 编辑
- # 客户端程序
- use IO::Socket;
- $socket = IO::Socket::INET->new(
- #PeerAddr => '192.168.125.10',
- PeerAddr => '127.0.0.1',
- PeerPort => 1500,
- Proto => 'tcp',
- Type => SOCK_STREAM
- )or die "Could not open port.\n";
- unless (defined($pid = fork())){die "Can not find fork\n"};
- if ($pid){
- while($line = <>){
-
- print $socket $line;
- }
- }
- # else{
- # while($line = <$socket>){
- print "$line\n";
- # }#此段程序一运行,客户端也不能向服务器发信息了,如何解决呢?这个例子是从书上照搬的
-
- # }
- close($socket);
- ############
- #服务器端程序
- use IO::Socket;
- $server = IO::Socket::INET->new
- (
- LocalPort => 1500,
- Type =>SOCK_STREAM,
- Reuse => 1,
- Listen => 5
- )or die 'Could not open port\n';
- while ( $client = $server->accept()) {
- unless (defined($pid = fork())){die "Cannot find fork"};
- if($pid){
- while($line = <$client>){
- print "$line";
- }
- }
- else{
- while($line = <>){
- print $client $line;
- }
- }
- }
- close($server);
复制代码 |
|