At 12:36 PM +0000 2/6/12, Marco van Kammen wrote:
Dear List,

I'm having difficulties with the following:

Say you have a server program that listens on port 1234
The server handles requests and spawns a child process for every connection.

If a client sends the text "foo" to the server, the server itself should try to make a connection with another service on port 4321.


Do you mean the parent server process here or the child server process? The parent process should still be listening on port 1234 if you have actually spawned a child process to read and write data from the remote client,.

How can I accomplish that the server still responds to incoming requests on port 1234 but also sends its commands over the other socket to port 4321?


You have basically three options for reading and writing from/to multiple sockets simultaneously:

1. Non-blocking sockets.
2. Use the Unix select function (implement in Perl in the IO::Select class)
3. Multi-tasking using either processes or threads.

The select function is probably the most efficient, allowing you to access multiple sockets from a single thread of execution without polling. Using non-blocking sockets requires you to continually check each socket for data. Multi-tasking may require you to solve the problem of sharing data between processes and threads, not always a straight-forward task.


I end up with my server talking over port 4321 but not listening to commands on port 1234 anymore..

I've found some people talking about IO::Socket:Select but I'm unable to find a simple example of how to implement something aboveŠ

I cannot find the IO::Socket::Select module, but IO::Select exists and can be used with sockets. Perl also has the 4-argument version of the select function, which calls the Unix function of the same name (see 'perldoc -f select' and scroll down past the 1-argument version).

All I want to accomplish is talking over 2 (or more) sockets without the server hanging itself on one of the sockets.

Any help with examples or pointers in the right direction is appreciated!

See the documentation provided with the IO::Select and IO::Socket modules. The idea is to use the select function with a timeout value to discover when any input is available on a socket, then read the data from that socket, process it, and execute the select function again. The select function blocks until there is data available. If there is no data ready when the timeout expires, select returns anyway with a "no data" status, and your program can do other processing, then call the select again to wait for input.


--
Jim Gibson
j...@gibson.org

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to