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

My problem is the following: I have written a simple TCP chat server using IO:Socket but once I get the server to listen, it will not go on to execute the rest of the program (because it waits for a connection). Is there any way to start the one sub routine of the server listening in the background?

Replies are listed 'Best First'.
Re: Background Sub?
by splinky (Hermit) on Jul 04, 2000 at 17:41 UTC
    This is actually a very big question. Let's say I could tell you how to move the listen to the background. What then? How would your program be notified when something happened on the socket? What if your program is doing something else when the notification comes in?

    So, the roundabout answer is, you probably don't want to do it that way. Use Fcntl to set your socket to non-blocking, and then check it occasionally to see if anything's connected. Or, if all you're doing is waiting for connections and processing input, don't worry about the non-blocking stuff. Use select and let it tell you when something interesting happens. IO::Select is a nice module for such things.

    BTW, Richard Stevens' _UNIX Network Programming_ is an invaluable resource for stuff like this.

    *Woof*

Re: Background Sub?
by ahunter (Monk) on Jul 04, 2000 at 19:05 UTC
    Hum, this is now definately a FAQ (not that it wasn't in the first place). See Socket.pm nonblocking

    Andrew.

    Update: You can also now go and see my tutorial on this subject, complete with very silly example.

Re: Background Sub?
by btrott (Parson) on Jul 05, 2000 at 01:10 UTC
    There's a tutorial on writing a chat server in Webmonkey that may help you. You need to use select to listen for sockets that have information waiting to be read.