Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Client socket code:use IO::Socket::INET; use strict; use warnings; # auto-flush on socket $| = 1; # creating a listening socket my $socket = new IO::Socket::INET ( LocalHost => '0.0.0.0', LocalPort => "1111", Proto => 'tcp', Listen => 1, Reuse => 1 ); die "cannot create socket $!\n" unless $socket; print "server waiting for client connection on port 1111\n"; while(1) { # waiting for a new client connection my $client_socket = $socket->accept(); # get information about a newly connected client my $client_address = $client_socket->peerhost(); my $client_port = $client_socket->peerport(); print "connection from $client_address:$client_port\n"; # read up to 1024 characters from the connected client my $data = ""; $client_socket->recv($data, 1024); print "received data: $data\n"; # write response data to the connected client $data = "ok"; #****************************** #Sending three messages but only one is reaching the client. sleep(2); $client_socket->send($data); sleep(2); $client_socket->send($data); sleep(2); $client_socket->send($data); #****************************** # notify client that response has been sent shutdown($client_socket, 1); } $socket->close();
Output:use IO::Socket::INET; # auto-flush on socket $| = 1; # create a connecting socket my $socket = new IO::Socket::INET ( PeerHost => '192.168.1.104', PeerPort => '1111', Proto => 'tcp', ); die "cannot connect to the server $!\n" unless $socket; print "connected to the server\n"; # data to send to a server my $req = 'hello world'; my $size = $socket->send($req); print "sent data of length $size\n"; # notify server that request has been sent shutdown($socket, 1); # receive a response of up to 1024 characters from server my $response = ""; $socket->recv($response, 1024); print "received response: $response\n"; $socket->close();
Any help will be very appreciated.received response: ok
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to insert delay between two server-client messages.
by SuicideJunkie (Vicar) on Nov 04, 2013 at 13:47 UTC | |
|
Re: How to insert delay between two server-client messages.
by Anonymous Monk on Nov 04, 2013 at 14:37 UTC |