LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Simple multi-threaded/forked TCP listener/server (https://www.linuxquestions.org/questions/programming-9/simple-multi-threaded-forked-tcp-listener-server-825698/)

systemlordanubis 08-12-2010 12:04 AM

Simple multi-threaded/forked TCP listener/server
 
Hi All,

For quite some time now, I've been trying to implement a multi-threaded or forked TCP server to perform the following action:


1) Listen for new connections on all ip addresses on a specific port.
2) Wait for a new connection to arrive
3) When a new connection comes in, fork or thread to free up the listening process so it can go back to #2.

When handling a client after it's been forked/threaded
4) Get the connected client's ip address.
5) Perform some in-house processing;
6) Read the data sent from the client (checking for http connections)
7) Perform some more processing;
8) Write some data back to the client based on the above processing
9) Close the socket/thread/fork


I've tried implementing an solution similar to the multi-threaded example show here http://perl.active-venture.com/pod/p...-sockets.html; as well as looking at the CPAN solutions Net::Daemon and POE::Component::Server::TCP but these don't seem to give me the information needed.

The active-venture solution provides me with what I need, but randomly exits without reason/error and when attempting to change the 'for' loop to a 'while' loop, it keeps throwing up different errors.

Thanks in advance for any help.
Anubis.

Wim Sturkenboom 08-12-2010 02:00 AM

Not sure what you're asking for. There is an example of a threaded server in C on Linux Sockets: Book Programs

systemlordanubis 08-12-2010 02:56 AM

Hi Wim,

Thanks for the reply. I'm actually looking for a solution in Perl and basically a server to listen to a TCP port for incoming connection and when a connection comes in, handle that connection in another thread to free up the main listening thread.

As mentioned, I've implemented a few examples and the one that's closest is multi-threaded example from active-venture, but it's randomly exiting; so I'm seeing if anyone has used or written a simple server that can accomplish these tasks.

Thanks
Anubis.


All times are GMT -5. The time now is 11:51 AM.