论坛徽章: 0
5 可用积分
现在我改了下程序了,不过发现接受数据有问题,为什么大的数据接收数据就会出错。。
这是代码:
my $ port = shift | | MY_SEG_PORT;
my $ protocol = getprotobyname ( 'tcp' ) ;
socket ( SOCK, AF_INET, SOCK_STREAM, $ protocol ) or die "socket() failed !" ;
setsockopt ( SOCK, SOL_SOCKET, SO_REUSEADDR, 1) or die "can't set SO_REUSEDDR: $!" ;
my $ my_addr = sockaddr_in( $ port , INADDR_ANY) ;
bind ( SOCK, $ my_addr ) or die "bind() failed !" ;
listen ( SOCK, SOMAXCONN) or die "listen()failed: $!" ;
warn "waiting for incoming connections on the segment $port...\n" ;
while ( 1)
{
next unless my $ remote_addr = accept ( SESSION, SOCK, ) ;
my ( $ port , $ hisaddr ) = sockaddr_in( $ remote_addr ) ;
warn "Connection from {" , inet_ntoa( $ hisaddr ) , ",$port}\n" ;
SESSION- > autoflush( 1) ;
my $ lengthStr = 0;
my $ data = "" ;
my $ senData = "" ;
my % TF = ( ) ;
my $ wordNum = 0;
my $ readLength = 2048;
my $ hasRead = 0;
sysread ( SESSION, $ lengthStr , 4) ; #文本长度
my $ length = unpack ( 'N' , $ lengthStr ) ;
warn $ length . "\n" ;
while ( $ hasRead < $ length )
{
my $ tempLength = ( $ hasRead + $ readLength ) < = $ length ? $ readLength : ( $ length - $ hasRead ) ;
sysread ( SESSION, $ data , $ tempLength , $ hasRead ) ;
$ hasRead + = $ tempLength ;
}
#read (SESSION,$data,$length,$hasRead);
syswrite ( STDOUT , $ data , $ length ) ;
#print "aaaaaaaaaaa\n";
#print OU $data."\n";
#$data=decode('utf8',$data);
#$data=encode('cp936',$data);
if ( bytes: : length ( $ data ) ! = $ length )
{
die "transfer error!\n" ;
}
[ 本帖最后由 earl808 于 2008-9-10 18:49 编辑 ]
我来回答
a.rar
5.37 KB, 下载次数: 14
里面有两个文件,分别是发送和接受的数据