Perl Socket Conditional不起作用

时间:2013-03-04 20:34:56

标签: perl sockets client-server conditional

我正在尝试创建一个服务器和客户端,其中服务器根据客户端发送的内容向客户端返回不同的消息。如果客户端建立连接但没有发送任何内容,服务器将返回消息1,并且如果客户端发送一些数据,服务器将返回消息2.但是这不起作用,客户端保持等待数据并且没有打印任何内容。 / p>

客户端:

use IO::Socket;

my $sock = new IO::Socket::INET (
    PeerAddr => '10.1.1.28',
    PeerPort => '7070',
    Proto => 'tcp' ); 

if (@ARGV != "") {
    print $sock "$ARGV[0] $ARGV[1]";

} else {
    $data = <$sock>;
    print $data;
}       

$sock->close;

服务器

use IO::Socket;
my $sock = new IO::Socket::INET (
    LocalHost => '10.1.1.28',
    LocalPort => '7070',
    Proto => 'tcp',
    Listen => '1',
    );

while(1) {
    my $new_sock = $sock->accept();

    if (<$new_sock> ne "") {        
        print $new_sock "conection with parameters";
    } else {
        print $new_sock "default message";  
};

1 个答案:

答案 0 :(得分:0)

需要选择

use IO::Socket; 
use Data::Dumper; 
my $sock = new IO::Socket::INET(
    LocalPort => '7070',
    Proto     => 'tcp',
    Listen    => '1',
);

while (1) { 
    my $new_sock = $sock->accept();
    my $in       = <$new_sock>;
    chomp($in);         
    if ( $in ne "" ) {  

        print Dumper($in);  
        print $new_sock "conection with parameters";
    }                       
    else {
        print $new_sock "default message";
    }
}