473,378 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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
client.

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.

thanks

Nov 21 '05 #1
3 2078
Hi

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.

CreateThread
http://msdn.microsoft.com/library/de...us/dllproc/bas
e/createthread.asp

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
memory.

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.

http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemnetsocketstcplistenerclasstopic.asp

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
request.

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

0
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...
0
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 ...
2
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...
4
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...
19
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...
3
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>...
14
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...
0
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.