Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Socket programming

by woutvc (Initiate)
on Nov 12, 2008 at 14:17 UTC ( #723154=perlquestion: print w/replies, xml ) Need Help??
woutvc has asked for the wisdom of the Perl Monks concerning the following question:

I have a server socket program
while(1) { &write_log(\*LOG,$program,"Waiting for client to connect..."); while($client = $server->accept()) { $client->autoflush(1); while (<$client>) { chomp; $buffer=substr($_,0,66); my ($time) = time; my $loctime = localtime($time); &write_log(\*LOG,$program,"SERV <- CLNT <$buffer>"); # Call FAB_TELEGRAM.PROCESS_V2 $sth = $dbh->prepare( q{ BEGIN :answer:= FAB_TELEGRAM.PROCESS(:buffer); END; } ); $sth->bind_param_inout( ":answer", \$answer, 195 ); $sth->bind_param_inout( ":buffer", \$buffer, 195 ); eval{ $sth->execute; }; print $client "$answer\n"; &write_log(\*LOG,$program,"SERV -> CLNT <$answer>"); &ora_commit($dbh); } &write_log(\*LOG,$program,"Waiting for client to connect..."); close $client; } &write_log(\*LOG,$program,"Waiting for counter $counter"); $counter = $counter+1; }
But if the client unexpectedly disconnects I get stuck and have to restart the program. How can I avoid this?

Replies are listed 'Best First'.
Re: Socket programming
by SuicideJunkie (Vicar) on Nov 12, 2008 at 15:34 UTC
    Please try to edit your posts to add/correct things instead of making new ones with the same question.

    It is definitely possible to have sockets die without notice, such that the while loop won't exit. I suffered that when I wrote a telnet-based multiplayer market trading game.

    What I did was to select from the current connections, and carefully read a byte at a time.
    Once there is enough to act on, I call whatever functions need calling. (If not, buffer the input so far, and loop back to the select)

    If one of the connections dies silently and unexpectedly, the select won't pick it to read from since there is no more input to read. After a few minutes of no activity, the dead/timed-out connections can be closed.
      If the connection is via a WAN or the Internet, then what you are describing can occur. Within a local network, it is much less likely. This is a TCP socket. When either side receives a FIN, the result in the application is an EOF. When a process dies, in almost all instances, open sockets are closed by the OS, resulting in a FIN going to the remote side. On a LAN, the receipt of this is almost certain. On a WAN, intervening equipment/hops can cause packet or session loss.
        Indeed it is a rare occurrence, but in a single-threaded server, one eternal wait event is all that is required to take the system down.
Re: Socket programming
by Illuminatus (Curate) on Nov 12, 2008 at 15:21 UTC
    If the client disconnects, then the while loop should terminate, because the next read will be an EOF. This code segment is obviously incomplete, and requires Oracle to execute. What happens if the client sends only a partial, or mal-constructed request? I would either add debug statements, or run in debug mode, to ensure it is hanging where you think it is hanging. Also, be sure to use
    use strict; use warnings;
    As this can reveal a multitude of sins...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://723154]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2018-04-20 04:15 GMT
Find Nodes?
    Voting Booth?