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

is the socket done or not?

by HaB (Sexton)
on Dec 04, 2000 at 21:43 UTC ( #44822=perlquestion: print w/ replies, xml ) Need Help??
HaB has asked for the wisdom of the Perl Monks concerning the following question:

Hrm...

I have been poking at this one all morning, and I can't seem to figure this one out. I have a little server app that recieves data from a corresponding client app. The problem is, the server can't seem to figure out when the client is thru sending data. The spec on the project is that the server close and regenerate the socket after each client is finished sending data, but it never happens. The client sends, and the server just sits. I have tried every method/module I can think of. Here is the latest incarnation using IO::Select.

Caveat: This is under Win32, which I am new to developing under. Not sure if that matters or not.

my $server = create_socket($config{'port'}); my $sel = IO::Select->new($server); while(1) { # straight out of perldoc IO::Select [-; while(@ready = $sel->can_read(1)) { my ($new); foreach $client (@ready) { if($client == $server) { $new = $server->accept(); $sel->add($new); } else { $msg_total = 0; while(<$client>) { $message = $_; print "\'$message\'\n"; # for logging: my $msg_length = length($message); $msg_total++; serv_out("Processing $msg_length bytes from $remot +e_ip\n"); # split the message into segments # we are splitting on carriage returns (NOT newli +ne!) @segments = split /\015/, $message; # call a sub to loop over the segments process_segment(\@segments, $client); } # end while(<$client>); debug_out("Closing connection..."); } $sel->remove($client); $client->close(); } } }


Any ideas?


-HaB


hword.

Comment on is the socket done or not?
Download Code
(tye)Re: is the socket done or not?
by tye (Cardinal) on Dec 04, 2000 at 23:07 UTC

    select() tells you that at least one byte (or end of file) is available to be read from a client. <$client> will block at least until it sees a newline or end-of-file. So there are lots of ways for your server to hang for at least a while. You may need to make your server more sophisticated.

    With sockets, you send end-of-file using close() or shutdown().

    For more ideas, I'd probably need to see the client's code as well.

    Updated to be a bit more correct about what select() tells you.

            - tye (but my friends call me "Tye")
Re: is the socket done or not?
by merlyn (Sage) on Dec 05, 2000 at 02:54 UTC
Re: is the socket done or not?
by Fastolfe (Vicar) on Dec 05, 2000 at 05:33 UTC
    I'm not sure, but this looks strange:
    while (<$client>) { # fetch based on newlines ... @segments = split /\015/, $message;
    So your code is expecting multiple lines (terminated by standard newlines) and is then splitting each "line" into different segments delimited by \r's ? That just seems weird. Be sure that $/ is what you expect it to be (so that <$client> works). If you need this to be more of a binary type of protocol, consider using sysread directly, read your data in binary chunks, and *then* split on your delimiter (making sure to keep the trailing bit around for the next time you read data, in case you get a partial command).
Re: is the socket done or not?
by repson (Chaplain) on Dec 05, 2000 at 06:47 UTC
    >> The spec on the project is that the server close and regenerate the socket after each client is finished sending data, but it never happens. The client sends, and the server just sits.

    The problem is that the in the while(<$client>) loop the server keeps on processing until EOF is recived from the client.
    However from your definition the client sends data, and then waits for the connection to be closed (which won't happen until it sends EOF).
    If the client were to close the connection after sending the server would stop trying to read from the client. Otherwise you need to send a specific prearranged EOF marker (specific character or string that won't occur in the data part of the transmission).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-08-29 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls