2009年11月16日星期一

[PerlChina] 求助 socket编程时碰到的问题

服务器端代码:
use strict;
use warnings;
use IO::Socket;

my $sock = IO::Socket::INET->new(
LocalHost => 'localhost',
LocalPort => 2345,
Proto => 'tcp',
Listen => 20,
Reuse => 1,
) or die "no socket: $!\n";
print "listening........\n";
while(1){
next if ( !( my $session = $sock->accept ) );
print "begin talking \n";
while ( my $data = <$session> ) {
print "reived from client : $data";
print $session "you said $data\n";
}
close $session;

}
客户端代码:
use strict;
use warnings;
use IO::Socket;

my $sock = IO::Socket::INET->new(
PeerAddr => "localhost:2345",
Proto => 'tcp',
) or die "no socket: $!\n";
my $line;
my $message;
while(1){
$line=<STDIN>;
print $sock $line;
while(<$sock>){
print "received from server: $_";
last;
}

}

---测试结果----
###################################################
客户端:
[root@localhost forkserv-oo-0.01]# perl client.pl
hello
received from server: you said hello
How are you
received from server:
are you listening?
received from server: you said How are you
oh I am puzzled
received from server:
oh no
received from server: you said are you listening?
####################################################
服务器端:
listening........
begin talking
reived from client : hello
reived from client : How are you
reived from client : are you listening?
reived from client : oh I am puzzled
reived from client : oh no
####################################################
现象分析:
似乎服务器返回的消息,每次都会多返回一个空回车符(或者换行符),导致消息延迟

困惑中.......
望高手不吝赐教

--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛"PerlChina Mongers 讨论组"论坛。
要在此论坛发帖,请发电子邮件到 perlchina@googlegroups.com
要退订此论坛,请发邮件至 perlchina+unsubscribe@googlegroups.com
更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

没有评论: