使用Perl中的Socket编程无法将数据发送到远程TCP端口

时间:2014-04-01 12:14:14

标签: perl sockets tcp-ip

我的程序:我打开一个文件“output.txt,readin gits最后一行将有字符串”PASS“或”FAIL“,如果找到PASS,那么我想发送字符串”OK“给服务器其他我必须发送字符串“ERROR”

问题观察:我无法接收远程服务器(IP 10.5.62.1)上的任何内容,但如果尝试使用Localhost(IP 127.0.0.1),我可以从客户端接收字符串。请帮助在远程服务器上接收字符串。

我收到以下错误

  

:send:无法在C:\ client.pl第44行确定对等地址   IO :: Socket :: send('IO :: Socket :: INET = GLOB(0x31cc20)','ERROR')调用于   C:\ client.pl第44行

     

客户代码:

#standard includes
use strict;
use warnings;
use diagnostics;
use IO::Socket::INET;
use IO::File;
use English;

#local variables
my $Socket;

#Create Socket for a specified port number
$Socket = new IO::Socket::INET ( 
                   PeerAddr => '10.5.62.1',
                               PeerPort => '4754', 
                               Proto => 'tcp', 
                               Listen => 1,
                               );
                   die "Could not create socket: $!\n" unless $Socket;

#open the file which is required , mapping file to INPUT Macro
open INPUT, "<output.txt";
#Reading all lines from a file and storing in @lines array
my @lines = <INPUT>;
close INPUT;
#Now @lines holds all the lines, one line in each element.
print "Last line is:\n";
#checking the last line in the file as it will have the verdict "Pass" or "Fail"
print $lines[-1];
if($lines[-1]=~ 'Pass')
{
  print "Setting verdict PASS and sending OK";
 #Sending Ok to Server on Ping Success
#  print $Socket "\r\nOK\r\n"; 
$Socket->send("\r\nOK\r\n");
  }
else 
  {
   print "Setting verdict FAIL and sending ERROR";
#Sending ERROR to Server on failure  
#  print $Socket "\r\nERROR\r\n";
$Socket->send("ERROR");
 }
 #Now closing the socket as my job is done  
close($Socket);
  

服务器代码:

    use IO::Socket;


my $sock = new IO::Socket::INET (
                                  LocalHost => '10.5.62.1',
                                  LocalPort => '4754',
                                  Proto => 'tcp',
                                  Listen => 1,
                                  Reuse => 1,
                                 );
die "Could not create socket: $!\n" unless $sock;
my $new_sock = $sock->accept();
       while(<$new_sock>) {
          print $_;
       }
      close($sock);

0 个答案:

没有答案