1

The static $_ might needs to be flushed. It is tried to be done by $|=1;.

enter image description here

DESIRED OUTPUT:

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

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;
    }
}
4

1 回答 1

4

服务器的输入循环包含:

print $clientsocket $_;

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

于 2013-04-18T06:51:36.313 回答