Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

socket sending/getting one message then nothing

by jptxs (Curate)
on Apr 11, 2001 at 05:11 UTC ( [id://71563]=perlquestion: print w/replies, xml ) Need Help??

jptxs has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple server and client. I pulled the code right out of something else I have that works perfectly. In this pair, though, the server gets the first message and then nothing. The server does not hang or zombie, just does not get any more messages from the socket. The code is almost boilerplate from the books...any ideas? server socket code:
my $socket = IO::Socket::INET->new( LocalPort => 1880, Type => SOCK_STREAM, Reuse => 1, Listen => 10 ) or die "Big Problem with the Server, Man : $!\n\n"; while ( $test ) { my $date = localtime(); warn "Started up: $date\n"; while ( my $client = $socket->accept() ) { my $message = <$client> ; warn "$message"; chomp $message; print $client 'Message recieved', "\n" if $message; if ( $message eq 'die' ) { #etc., etc. } } }
then I have a little client I'm using to test it:
use IO::Socket::INET; my $socket; # for use in all scopes $socket = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => '1880', Proto => "tcp", Type => SOCK_STREAM) or die "Big Problem connecting to Server, Man : $!\n\n"; while (1) { print 'talk to me, like keyboards do: '; my $input = ; chomp $input; if ( $input =~ /^q/i ) { exit; } else { print $socket "$input\n"; } }
"A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche

Replies are listed 'Best First'.
Re: socket sending/getting one message then nothing
by chipmunk (Parson) on Apr 11, 2001 at 08:33 UTC
    Well, your server never looks for any further messages from the client. After establishing a connection, the server prints the first line of data from that client, and then just goes back to wait for a new connection from another client.

    You need to have some sort of loop which looks for additional input from the current client. If you plan on handling multiple clients at the same time, you will probably want to use select (or IO::Select). perlipc should prove helpful as well.

    P.S. Your code appears to have been slightly munged, around the lines where input is being read.

      thanks, chipmunk. that, indeed was it. the place I pulled this code from loops through and destroys and recreates the socket when it talks to the server - i.e. there are no persistant connections. So there it was not an issue. wasn't thinking it all the way through when I pulled it out to test for this next application. =)
      "A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://71563]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 13:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found