473,695 Members | 1,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best design pattern for a TCP "HUB" service

i need to build a service that will accept incoming TCP/IP connections.
the service should act like a "HUB" where on one side clients connect
to it and stay connected for as long as they like and on the other side
the service reads messages for these clients from MSMQ and sends them
to already connected clients. the clients can also send information
back to the "HUB" which will be parsed and sent to MSMQ.
when the clients connect to this service, they send their unique
identity to the server over the TCP/IP stream (the client ip is NOT
fixed so i can't use that for authentication) .

i thought of 2 designs for this:
design 1:
1. have a class that listens for incoming connections (using the
TcpListener class).
2. when an incoming connection arrives, it accepts it and spawns a new
thread with the the incoming connection. the thread will receive the
incoming identity and then store itself (the thread class instance
along with the client's identity in a hashtable). then the thread
(class) will wait for more data from the client or will send data to
the client if the thread on item #3 calls a send data method.
3. have a class that periodically reads the MSMQ for messages to
clients. get the target client unique id, find the appropriate class in
the hash table and call the send data method for the specific class
(which in theory should still hold the connection open).

design 2:
1. have a class that listens for incoming connections (using the
TcpListener class).
2. when an incoming connection arrives, it accepts it and spawns a new
thread that will just read the unique id and add to the hashtable the
connection and the unique id as the key. this thread is then destroyed.
3. have another thread that will scan the hashtable all the time and
look for incoming data, read it and parse it.
4. have a thread than reads MSMQ for messages to the clients, pulls the
correct connection from the hashtable and send the message to the

both designs should work but i would like to hear ideas and/or comments
about the designs.

what is the maximal number of threads i can spawn? max number of TCP/IP
clients the service can support?

i need a design that will be able to support as many clients as
possible but on the same time not be too demanding on server resources.


Nov 21 '05 #1
3 2094

According to the MSDN below,

The number of threads a process can create is limited by the available
virtual memory. By default, every thread has one megabyte of stack space.
Therefore, you can create at most 2028 threads. If you reduce the default
stack size, you can create more threads. However, your application will
have better performance if you create one thread per processor and build
queues of requests for which the application maintains the context
information. A thread would process all requests in a queue before
processing requests in the next queue.

e/createthread.as p

For the maximum socket a TCPLister can accept, in theory it is 2^32-1, we
can consider it as unlimited, because every time we create a socket, the
system will allocate kernel memory for the socket handle, so the accutal
account is hard to determine, because the other program will also use the
handle(e.g. file handle, gdi hanle ...) which will share the system kernel

In Socket programming, we have another term named backlog, which means the
connection request queue length before we accept it.

Use the Start method to begin listening for incoming connection requests.
Start will queue incoming connections until you either call the Stop method
or it has queued MaxConnections. Use either AcceptSocket or AcceptTcpClient
to pull a connection from the incoming connection request queue. These two
methods will block. If you want to avoid blocking, you can use the Pending
method first to determine if connection requests are available in the queue.

frlrfsystemnets ocketstcplisten erclasstopic.as p

Backlog Parameter
Windows Sockets server applications generally create a socket and then use
the listen() function on the socket to receive connection requests. One of
the parameters passed when using the listen() function is the backlog of
connection requests that the application would like Windows Sockets to
queue for the socket. The Windows Sockets 1.1 specification indicates that
the maximum allowable value for backlog is 5; however, Microsoft? Windows
NT? version 3.51 accepts a backlog of up to 100, Microsoft? Windows NT? 4.0
Server and Windows 2000 Server accepts a backlog of 200, and Microsoft?
Windows NT? 4.0 Workstation and Windows 2000 Professional accepts a backlog
of 5 (which reduces the memory footprint).

The backlog is usually used to consider the concurrent TCP connection

Since newsgroup support did not provide advisory service, I just give some
general idea based on my knowlege.
If the maximum clients is not large, e.g. 100, you may try to use the first
design, because that is easy to implement, every client will have a
specified thread to do that.

While if the maximum client will be 1000 and more, according to the thread
count limitation, the second one will be OK. As a suggestion, if you want
to increase the performance, you may create more than one thread to query
the hashtable to handle the connection.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #2
Hi Peter,

thank you very much for the overview and recommendations ! since the
client will try to connect if his connection request fails then the
backlog is not too much of a concern altough i would like to accept all
incoming connections all the time.
based on your recommendations i think i would go with option 2 wich
will take a bit more writing but will support a large amout of clients
(if i get to a large enough number then i am rich and can buy another
server for clients to connect :-)

again thanks for all your help!
Dan Avni

Nov 21 '05 #3
Hi Dan,

I am glad that my suggestion helped you.
If you still have any concern, please feel free to post here! :)
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed Pentagjetvedeh karuvificials madhla reachathe strategy in karkun campaign deshatinst terrorism. "mudivae maretu winning or losing karkun global varti jetvedeh terror?" Mr. Rumsfeld adugued in a recent memormariyuum. vede velli jetvedeh madhla...
by: MarionEll | last post by:
XML 2004 to Focus on Government XML Applications Expanded Government Track to Highlight XML Applications in U.S. and Abroad; XML.gov, KM.gov, CIO Council, and SICoP to Co-Host Event Alexandria, Va. – Oct. 6, 2004 – IDEAlliance, a leading industry association dedicated to fostering XML and other information technology standards, today announced that XML Conference and Exposition 2004 will feature a full track of presentations and...
by: ZubZero | last post by:
Hello, i have to write a utility in c++ for windows 2k/XP. But i have 2 difficult problems. I asked many programmers i know, but none of them was able to tell me how i can do this. 1. I would like to disable a specific USB-port (not all ports, only one). I have a USB Cardreader with an integrated 7-port USB-Hub.
by: Andy Leszczynski | last post by:
watch this: http://www.turbogears.org.nyud.net:8090/docs/wiki20/20MinuteWiki.mov or read this: http://www.turbogears.org.nyud.net:8090/docs/wiki2 0/page4.html should not it be: 2 def save(self, pagename, data, submit, new): 3 hub.begin()
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has one, and only one parent. The depth should not exceed 6 or 7 levels. The initial import will have about 6 million leaves, and 3 million branches. I would expect the leaves to grow significantly, in number easily tripling. However, the branches will...
by: K R | last post by:
Hi, I have generated this XML from my application. But, when I open this XML, it is throwing error. Please help me to resolve this. <?xml version="1.0" encoding="utf-8" ?> <searchResults> <totalhits>16916</totalhits> <searchquery>*</searchquery> <timetaken>TIME TAKEN</timetaken>
by: David W. Fenton | last post by:
I'm no stranger to this error message, but I have a client who is experiencing it, but, fortunately, without any actual data corruption, and it's driving them made. Their inability to grasp that the cause is fundamentally a networking problem is also making me pull my hair out. Some history: The network is composed of five computers dating back to 1995, each purchased one at a time, and each having a different OS, etc. Until
by: PaulB | last post by:
Microsoft Access Performance FAQ (Last updated 2006/01/23) Try the following suggestions as originally suggested by Frank Miller of Microsoft PSS and extensively updated by me. Almost all of these tips also apply to Microsoft Access 97. The three most common performance problems in Access 2000 are: - LDB locking which a persistent recordset connection fixes
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.