$ _输出额外的数据位

时间:2013-04-18 06:13:41

标签: perl network-programming client-server

静态$ _可能需要刷新。它试图通过$ | = 1;来完成。

enter image description here

渴望的输出:

客户端

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$

代码:

客户

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 : $_";
    }
}

服务器

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

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

print "Waiting for the Client.\n";
server_loop();
$socket->close();

sub server_loop
{
OUTER:
    while (my $clientsocket = $socket->accept())
    {
        print "connected from : ", $clientsocket->peerhost();
        print ", port : ", $clientsocket->peerport(), "\n";

INNER:
        while (<$clientsocket>)                                 #
        {                                                       #
            print"Message received from Client : $_";           #
            last INNER if ($_ eq "\$END\$\n");                  #       RECEIVING
            #last OUTER if ($_ eq "\$QUIT\$\n");                #
            print $clientsocket $_;                             #
        }                                                       #

        my $login_ack = "login_ack.txt";
        open LOGINACK, '<', $login_ack or die "Cannot open login acknowledgment file $login_ack ($!)\n";
        my @loginack = <LOGINACK>;
        close LOGINACK;

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

        foreach my $loginack (@loginack)
        {
            $|=1;
            print $clientsocket $loginack;
            print LOG "SERVER : $loginack";
            print "server to client : $loginack";
            #last if ($loginack eq "\$END\$\n");
        }

        close LOG;
        close $clientsocket;
    }
}

1 个答案:

答案 0 :(得分:4)

服务器的输入循环包含:

print $clientsocket $_;

因此,客户端发送到服务器的所有内容都将在login_ack.txt文件之前发送回客户端。因此客户端将其打印为从服务器收到的消息。