LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-14-2011, 02:29 PM   #1
PinoyAko
LQ Newbie
 
Registered: May 2011
Posts: 28

Rep: Reputation: 0
Building a server


I am planning to code a tcp/udp server. The program is required to accept tcp and udp connections so I need both protocols running on the server.

I have 3 plans on how I would build the server and I am not sure which one would be better.

Plan 1: The program would open both tcp and udp sockets and then use select() on both.

Plan 2: The program would create 2 threads. one thread is for the TCP and the second one is for the UDP. I would still use select().

Plan 3: Make the program check for an argument that would signal the program to either start as udp or tcp server. So this would mean two instance of this program would be running one would be listening on tcp and the other on udp.


Which would be better? I would be using select() to check if I data is available.
 
Old 05-14-2011, 11:31 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Ok, now that the thread hijack is over, let's return to:
Quote:
Originally Posted by PinoyAko View Post
Plan 1: The program would open both tcp and udp sockets and then use select() on both.

Plan 2: The program would create 2 threads. one thread is for the TCP and the second one is for the UDP. I would still use select().

Plan 3: Make the program check for an argument that would signal the program to either start as udp or tcp server. So this would mean two instance of this program would be running one would be listening on tcp and the other on udp.
There might be factors of which I'm unaware in your situation, but just with what I know of your question, I vote for 3, then 1, then 2.

1 is superior to 2 because at any given time you're responding to one event, so you don't need to worry about synchronizing access to data.

But assuming that the TCP server and the UDP server don't share any data, 3 is superior to 1, because you don't have two otherwise independent chunks of code sharing the same heap. This makes the code easier to debug in case of buffer overrun. Otherwise, for example, the UDP server code might trash the heap but still be smiling with glistening teeth as the TCP server code dies because of the heap corruption.
 
Old 05-14-2011, 11:40 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
deleted

Last edited by dugan; 05-15-2011 at 04:14 PM. Reason: refers to a post that was deleted
 
Old 05-15-2011, 09:56 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
So, to summarize your question, you want to know whether it is better to use a single thread, or to use multi-threading or to use multi-processing (although you didn't suggest the fourth possibility, which is to fork() a new process within your single application). To answer this question is only possible when we know the overall makeup of your application. If there was always one best method, then everyone would always use it, and the others would probably not exist.
If the handlers for the two servers have much overlap (shared data or code), then single-threading or multi-threading is probably best. Since you've considered the possibility of multi-processing, I'll surmise that the two servers have little or no coupling, so a multi-processing approach may well be your best bet.

--- rod.
 
Old 05-16-2011, 10:57 AM   #5
PinoyAko
LQ Newbie
 
Registered: May 2011
Posts: 28

Original Poster
Rep: Reputation: 0
I decided to go with multi-processing since they don't share data. I also think it is much safer that way because of wje_lq's post.
 
  


Reply



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
Building my first home server - atom based router/file server build need advice mothergoose729 Linux - Networking 5 03-23-2010 03:19 AM
Building web server server filsed Linux - Software 1 07-15-2005 01:56 AM
building new server. Ratclaws Linux - Hardware 2 10-19-2004 05:07 PM
building a server hamza11050 Linux - Networking 8 09-23-2004 10:21 PM
Building A Server Crashed_Again Linux - Hardware 5 02-27-2003 12:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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