perl accept()

I think a common Perl socket idiom (used repeated in my scripts) is

while ( accept(Client,Server) ) {
# use Server and Client file handles
close Client;
close Server;

I think the while loop would run only once (P 442 [[ programming perl ]]). The first time it runs, accept() blocks until a client connects. After the connect, the loop runs for n seconds, the duration of the connection. At end of the connection, this script closes Client, exits the loop (?), and closes Server stream.

How does the server continue to service other clients? I think fork and exec is the standard solution, spawning a new process. In contrast, Java spawns a new thread — perhaps more efficient.

Remember a single-thread NIO server can handle thousands of sockets, one for each concurrent client. Even a traditional IO server can spawn hundreds of threads.

Hundreds of “processes” sounds like too heavy for an ordinary OS. Apache httpd in our servers typicall spawn dozens of child processes.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s