Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-12-2010, 12:04 AM   #1
Registered: Jun 2010
Distribution: Debian, Ubuntu, Win
Posts: 141

Rep: Reputation: 16
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; as well as looking at the CPAN solutions Net:aemon 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.
Old 08-12-2010, 02:00 AM   #2
Wim Sturkenboom
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Not sure what you're asking for. There is an example of a threaded server in C on Linux Sockets: Book Programs
Old 08-12-2010, 02:56 AM   #3
Registered: Jun 2010
Distribution: Debian, Ubuntu, Win
Posts: 141

Original Poster
Rep: Reputation: 16
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.



server, socket

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
multi-threaded server, pthreads, sleep Parahat Melayev Programming 6 01-17-2012 06:20 AM
Multi-threaded Chat Server sci3ntist Programming 4 10-11-2007 05:49 AM
pthreads - multi-threaded server elitecodex Programming 1 08-20-2005 03:58 PM
how to do waitpid() using a multi-threaded server? Thinking Linux - Software 1 04-13-2005 09:50 PM
Segementation fault on Multi threaded server on ia-64 Linux Latha Linux - Software 3 09-09-2004 06:40 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:59 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration