Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 28th, 2007, 7:45 AM   #1
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
TCP Server Design

I am working on a project which uses a client/server model. The client will connect with the server using a TCP/IP connection (though I have considered using UDP/IP).

For server design though there are two ways I have figured I can go with the design assuming that I stick with TCP/IP sockets:

Threaded Blocking Sockets:

One main thread watches for incoming connections and managed a pool of threads. When an incoming connection is heard, one of the threads is signalled to take over to complete the connection and authenticate the client.

Pros are that the model is fairly simple, it is easy to manage individual sessions. And any work that needs to be done can be placed into a Producer/Consumer Queue for processing. Also this would mean little latency on the connections.

The cons are that it consumes a large number of resources, and I am not really sure what the maximum number of threads I can expect the service to load, the C# thread pool object (which I am not using) has a default maximum of 25, which really is not much.

Additionally if I place work in the processing queue I have to wait for work to be complete, which means no more socket activity until something is returned, or the sockets now have to become non-blocking so that I can also simultaneously check for work results.

Non-blocking Server Approach:

With a TCP/IP system this would mean establishing a connection and placing it into some sort of list, traversing the list polling each of the sockets for data. When data is received, place the work in the queue and check this after polling all sockets for work results, sending data back on the appropriate sockets when complete.

This uses many less threads, however poses a problem with latency. Latency probably isn't a large concern, but I still want to keep it at a minimum. After so many connections I would have to spawn other threads to monitor data, so I don't overload the polling process.

UDP would take a similar approach, only it would not be necessary to keep a list of active connections, for obvious reasons.

Anyone have any insight on which approach would be the best?
__________________
Clifford Matthew Roche <geek@cliffordroche.com>
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Nov 28th, 2007, 9:37 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Re: TCP Server Design

It would really depend on the load of the transactions and what level of system resources you have available.

Threaded Blocking Sockets would be your best bet if you are looking to process transactions quickly. If you are expecting >25 connections then hopefully you have a way around the thread pool object limitation. I'm thinking the resource consumption would be limited and would fluctuate within acceptable ranges (based on current hardware) in relation to the rise and fall of the transaction load.

If the resource consumption became a problem and you would be willing to take a slight hit in speed, you could go with the Non-blocking Server Approach and try to allocate resources on-demand.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Nov 28th, 2007, 6:55 PM   #3
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: TCP Server Design

Why do you need to use polling if you're using non-blocking sockets? By passing a unique object to BeginReceive(), such as the receiving socket or your own user-defined type (representing a connected client, for example), you have a mechanism for determining which object has the read completed when your callback is invoked. Depending on what you want to do, you can handle it in the callback method, or you can raise an event for your program to handle.

If you do raise an event for handling, though, be careful not to overwrite the just-received data in the buffer when making a new call to BeginReceive() in your callback. Either put the bytes into a new portion of the buffer (you can use it like a circular buffer in this regard), or allocate a new buffer (less efficient and perhaps needlessly complex, as you need to store a reference to this buffer for each read operation).
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 28th, 2007, 8:54 PM   #4
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
Re: TCP Server Design

If you are using BeginReceive, then you are using asynchronous sockets. I prefer to stick with synchronous sockets.
__________________
Clifford Matthew Roche <geek@cliffordroche.com>
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Nov 28th, 2007, 8:56 PM   #5
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
Re: TCP Server Design

IR: in retrospect, I will have very few working threads as most client server connectivity will be very minor "check for updates" kind of events. However a couple of clients will be sending large amounts of binary data.

I think you are right, a low latency setup will be best here for me, and I shouldn't need to spawn too many threads to get this done.

The default threadpool object isn't powerfull enough for me to use outside of the Producer/Consumer queue, so I will have my own ThreadPool object, but in both circumstances you can change the max number of threads from 25 to whatever limit your hardware will allow.
__________________
Clifford Matthew Roche <geek@cliffordroche.com>
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Nov 29th, 2007, 12:30 PM   #6
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Re: TCP Server Design

"Check for updates" hmmm? Why not download a text file containing the latest version number and link from a web server. That's just a few lines.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with C# TCP/IP Server csrocker101 C# 3 Mar 15th, 2007 12:32 AM
The Black Art of Video Game Console Design lostcauz Book Reviews 0 Apr 26th, 2006 7:31 PM
FTP Server in Python Sane Python 1 Mar 31st, 2006 10:25 PM
http server in c# lucifer Existing Project Development 4 Dec 1st, 2005 2:04 PM
send() problem w/ http server kch_86 C 2 Nov 25th, 2005 12:53 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC