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.


In reply to socket communication by ashishzankar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.