2013-04-18 55 views
1

靜態$ _可能需要刷新。它試圖由$ | = 1;來完成。

enter image description here

期望的輸出:

CLIENT

Connected to the Server. 
sent to server : SAS4 
sent to server : 50 
sent to server : SAS_ACTION LOGIN 
sent to server : LOGIN bss 
sent to server : PASSWORD cleint 
sent to server : $END$ 
Message received from Server : SAS4 
Message received from Server : 61 
Message received from Server : SAS_ACTION LOGIN_ACK 
Message received from Server : ACK_STATUS 0 
Message received from Server : ACK_MESSAGE Logged In 
Message received from Server : $END$ 

SERVER

Waiting for the Client. 
Connected from : 127.0.0.1, Port : 1862 
Message received from Client : SAS4 
Message received from Client : 50 
Message received from Client : SAS_ACTION LOGIN 
Message received from Client : LOGIN bss 
Message received from Client : PASSWORD cleint 
Message received from Client : $END$ 
server to client : SAS4 
server to client : 61 
server to client : SAS_ACTION LOGIN_ACK 
server to client : ACK_STATUS 0 
server to client : ACK_MESSAGE Logged In 
server to client : $END$ 

CODE:

CLIENT

use strict; 
use warnings; 
use IO::Socket::INET; 

my $socket = new IO::Socket::INET (PeerHost => '127.0.0.1', PeerPort => '1055', Proto => 'tcp', Reuse => 1) or die "$!\n"; 

print "Connected to the Server.\n"; 

send_login(); 
$|=1; 
receive_loginack(); 
$socket->close(); 

sub send_login 
{ 
    my $login_txt = "Login.txt"; 
    open LOGIN, '<', $login_txt or die "Cannot open $login_txt $!\n"; 
    my @login = <LOGIN>; 
    close LOGIN; 

    my $logfile = "logfile.txt"; 
    open LOG, '>>', $logfile or die "Cannot open $logfile ($!)\n"; 

    foreach my $login (@login) 
    { 
     print $socket $login; 
     print LOG "CLIENT : $login"; 
     print "sent to server : $login"; 
     #last if ($login eq "\$END\$\n"); 

    } 
    close LOG; 
} 

sub receive_loginack 
{ 
    while (<$socket>) 
    { 
     print"Message received from Server : $_"; 
    } 
} 

服務器

​​

回答

4

服務器的輸入迴路包含:

print $clientsocket $_; 

因此,客戶端發送給服務器的所有內容都將在login_ack.txt文件之前發送回客戶端。所以客戶打印它作爲從服務器收到的消息。

+0

謝謝你關注此事。回到ans時,當我註釋掉Server中的發送部分(foreach循環)時,客戶端仍然打印該特定部分。 我的看法是,它是從客戶端收到的。 – 2013-04-18 07:06:37

+0

不,沒有任何東西會自動導致客戶端讀回它發送的內容。 Flushing與它無關,流的兩個方向是獨立的。 – Barmar 2013-04-18 07:11:30

+0

ohh。那麼情況怎麼處理呢? – 2013-04-18 07:14:34