perl - $_ outputting extra bit of data -
the static $_ might needs flushed. tried done $|=1;.
desired output:
client
connected server. sent server : sas4 sent server : 50 sent server : sas_action login sent server : login bss sent server : password cleint sent server : $end$ message received server : sas4 message received server : 61 message received server : sas_action login_ack message received server : ack_status 0 message received server : ack_message logged in message received server : $end$
server
waiting client. connected : 127.0.0.1, port : 1862 message received client : sas4 message received client : 50 message received client : sas_action login message received client : login bss message received client : password cleint message received client : $end$ server client : sas4 server client : 61 server client : sas_action login_ack server client : ack_status 0 server client : ack_message logged in server client : $end$
code :
client
use strict; use warnings; use io::socket::inet; $socket = new io::socket::inet (peerhost => '127.0.0.1', peerport => '1055', proto => 'tcp', reuse => 1) or die "$!\n"; print "connected server.\n"; send_login(); $|=1; receive_loginack(); $socket->close(); sub send_login { $login_txt = "login.txt"; open login, '<', $login_txt or die "cannot open $login_txt $!\n"; @login = <login>; close login; $logfile = "logfile.txt"; open log, '>>', $logfile or die "cannot open $logfile ($!)\n"; foreach $login (@login) { print $socket $login; print log "client : $login"; print "sent server : $login"; #last if ($login eq "\$end\$\n"); } close log; } sub receive_loginack { while (<$socket>) { print"message received server : $_"; } }
server
use strict; use warnings; use io::socket::inet; $socket = new io::socket::inet (localhost => '127.0.0.1', localport => '1055', proto => 'tcp', listen => 1, reuse => 1) or die "oops: $! \n"; print "waiting client.\n"; server_loop(); $socket->close(); sub server_loop { outer: while (my $clientsocket = $socket->accept()) { print "connected : ", $clientsocket->peerhost(); print ", port : ", $clientsocket->peerport(), "\n"; inner: while (<$clientsocket>) # { # print"message received client : $_"; # last inner if ($_ eq "\$end\$\n"); # receiving #last outer if ($_ eq "\$quit\$\n"); # print $clientsocket $_; # } # $login_ack = "login_ack.txt"; open loginack, '<', $login_ack or die "cannot open login acknowledgment file $login_ack ($!)\n"; @loginack = <loginack>; close loginack; $logfile = "logfile.txt"; open log, ">>", $logfile or die "cannot open $logfile ($!)\n"; foreach $loginack (@loginack) { $|=1; print $clientsocket $loginack; print log "server : $loginack"; print "server client : $loginack"; #last if ($loginack eq "\$end\$\n"); } close log; close $clientsocket; } }
the server's input loop contains:
print $clientsocket $_;
so client sends server being sent client, before login_ack.txt file. client prints message received server.
Comments
Post a Comment