ashishzankar has asked for the wisdom of the Perl Monks concerning the following question:
I have written a socket program to send and receive two numbers and receive the same two numbers. The output only shows that the server has sent first number and also the client does not acknowledge that it has received the first number or the second number.
Server Program :use warnings; use strict; use IO::Socket; $| = 1; my ($client_socket, $received_data1, $received_data2, $data1, $data2, +$socket, $peer_address, $peer_port, $child); $socket = new IO::Socket::INET ( LocalHost => '192.168.56.102', LocalPort => '5000', Proto => 'tcp', Listen => 5, Reuse => 1 ); die "Coudn't open socket" unless $socket; system("clear"); print "Server is up and ready..."; while(1) { $client_socket = ""; $client_socket = $socket->accept(); $peer_address = $client_socket->peerhost(); $peer_port = $client_socket->peerport(); print "\n\nconnection established from '$peer_address'"; $child = fork(); unless($child) { while (1) { $client_socket->recv($received_data1, 1024); $client_socket->recv($received_data2, 1024); if(($received_data1 || $received_data2) eq 'stop') { close $client_socket; print "\n\nconnection closed by '$peer_address'"; exit(0); } $data1 = $received_data1; $data2 = $received_data2; print "\nRECIEVED from $peer_address: $data1\n"; print "\nRECIEVED from $peer_address: $data2\n"; $client_socket->send($data1); print "\nSent data to $peer_address : $data1\n"; $client_socket->send($data2); print "\nSent data to $peer_address : $data2\n"; } } close $client_socket; } CLIENT Program : use strict; use warnings; use IO::Socket; system("clear"); my ($socket, $send_data1, $send_data2, $received_data1, $received_data +2); $socket = new IO::Socket::INET ( PeerAddr => '192.168.56.102', PeerPort => 5000, Proto => 'tcp', ) or die "Couldn't connect to Server\n"; while (1) { print "enter first number (enter 'stop' to quit) : "; $send_data1 = <STDIN>; chop($send_data1); print "enter second number (enter 'stop' to quit) : "; $send_data2 = <STDIN>; chop($send_data2); $socket->send($send_data1); shutdown($socket, 1); $socket->send($send_data2); shutdown($socket, 1); if(($send_data1 || $send_data2) eq 'stop') { close $socket; die "Bye\n"; } $socket->recv($received_data1, 1024); $socket->recv($received_data2, 1024); print "From server : $received_data1\n"; print "From server : $received_data2\n"; } close $socket;
Actual output : SERVER output : Server is up and ready... connection established from '192.168.56.102' RECIEVED from 192.168.56.102: 1 RECIEVED from 192.168.56.102: Sent data to 192.168.56.102 : 1 you can see server is sending only one number or the first number. CLIENT output : admin1@ubuntu:~/perlscripts$ perl tcpclient.pl enter first number (enter 'stop' to quit) : 1 enter second number (enter 'stop' to quit) : 2 admin1@ubuntu:~/perlscripts$ this is the client side output, client does not say that it has received the two numbers sent by the server. Please help on this . Thanks in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: socket communication
by NetWallah (Canon) on Mar 18, 2014 at 17:45 UTC |