2016-11-25 37 views
0

我已經發現兩種類型的基於所述IO ::插座:: INET模塊,其接收並經由IP地址發送消息上網站程序。使用IO ::插座:: INET接收和發送在消息單個提示

接收郵件程序:

use IO::Socket::INET; 
my $text; 
$MySocket=new IO::Socket::INET->new (LocalPort=>1234,Proto=>'udp'); 
while(1) 
{ 
    $MySocket->recv($text,128); 
    $hostip=$MySocket->peerhost(); 
    if($text ne '') 
    { 
     print "\nReceived message from $hostip: $text \n"; 
     print "Command Output:\n"; 
     #system("$text"); 
     print "\n"; 
    } 
    else 
    { 
     print "Client has exited!"; exit 1; 
    } 
} 

發送消息:

use IO::Socket::INET; 
#Enter Destination IP Message 
print "Please Enter the destination IP: \n"; 
$DestinationIP = <STDIN>; 
chomp $DestinationIP; 

#Enter message to sent to Server 
print "Please Enter your message: \n"; 
$MySocket=new IO::Socket::INET-> new(PeerPort=>1234,Proto=>1234,Proto=>'udp',PeerAddr=>$DestinationIP); 
#$MySocket->send($msg); 

while($msg=<STDIN>) 
{ 
    chomp $msg; 
    if($msg ne '') 
    { 
     print "\n Sending $msg"; 
     if($MySocket->send($msg)) 
     { 
      print "done \n"; 
      print "\nPlease Enter another message:"; 
     } 
    } 
} 

我的問題是如何處理/合併在一個單一的MS-DOS提示符下運行這些程序。

對於如:

`Receiving and sending message in Single Prompt.` 

我真誠的道歉,如果我曾問過任何假設沒有意義。

+0

所以先把話說清楚,你只是想運行在接收和在一個腳本發送?不需要運行2個程序,但只有一個? – 2016-11-25 13:15:52

+0

@Gerry:是的。絕對是聊天。 – ssr1012

+0

好吧,讓我迅速發佈一個答案。還有一個問題,你是否想使用收到消息中的任何值通過發送功能發送? – 2016-11-25 13:18:58

回答

1

見旁邊的註釋中第23行和26行

use strict; 
use warnings; 
use IO::Socket::INET; 
my @Destination; 
my $text; 
my $Receive_Socket=new IO::Socket::INET->new (LocalPort=>1234,Proto=>'udp'); 
while(1) 
{ 
$Receive_Socket->recv($text,128); 
my $hostip=$Receive_Socket->peerhost(); 
if($text ne '') 
{ 
    print "\nReceived message from $hostip: $text \n"; 
    print "Command Output:\n"; 
    #system("$text"); 
    print "\n"; 

} 
else 
{ 
    print "Client has exited!"; exit 1; 
} 
push @Destination, $hostip; # Push the IP Received to where you will send it 
} 
my $DestinationIP = $Destination[0]; # Here we assign the original $hostip to $DestinationIP 
chomp $DestinationIP; 

print "Please Enter your message: \n"; 
my $Send_Socket=new IO::Socket::INET-> new(PeerPort=>1234,Proto=>1234,Proto=>'udp',PeerAddr=>$DestinationIP); 
while(my $msg=<STDIN>) 
{ 
chomp $msg; 
if($msg ne '') 
{ 
    print "\n Sending $msg"; 
    if($Send_Socket->send($msg)) 
    { 
     print "done \n"; 
     print "\nPlease Enter another message:"; 
    } 
} 
} 
+0

我的問題是,如果有人跟我,我怎麼能輸入IP地址聊天嗎? – ssr1012

+0

IP地址來自哪裏?在接收的$文本中?對此我不清楚。 – 2016-11-25 13:44:45

+0

'$ Destination' ...? – ssr1012