Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a singleton object that clients use with tcp remoting,
now before they get access to the object I try to limit them as so only two clients can use the program and all the others will wait in some queue or so.
How to do it?
Posted
Comments
John Orendt 26-Jun-12 6:02am    
what have you done so far?
Member 7966831 26-Jun-12 6:16am    
googled it

1 solution

In my approach, the best schema should be based on your application-level protocol you always have in such applications (even if you as a developer unaware that you introduce such protocol, you always do it, even though such protocol could be trivial; please see: http://en.wikipedia.org/wiki/Application_layer[^]).

The protocol should have a provision of some "denial" notification. In other words, you always accept all connections, but for the new connection beyond your established connection limit, you send the notification they the server is too busy and will stop further communication with this client; then you can disconnect from the server side.

Just in case, I would warn you against the idea of association of a client with a separate thread. The minimal number of communication threads you need to have on the server part is two: one is accepting all new connections, another one used to read/write from/to the network streams to communicate with all the clients. These to thread should communicate via the shared data collection holding the set of all clients. Each element of such collection should hold either a remote socket representing a remote client, or an instance of TcpClient, also representing a remote client. It depends on what do you use, System.Net.Sockets.TcpListener.AcceptSocker or System.Net.Sockets.TcpListener.AcceptTcpClient. Please see:
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx[^].

You can find further detail in my past answer:
Multple clients from same port Number[^].

If you use a schema with "denial" notification, you may need to store additional information in the collection I mentioned, such as a client status, for example "to be disconnected". A second service thread (the one maintaining the application-level protocol) should read the status, and, in case of "to be disconnected" status, send a "denial" notification and close the connection.

This is just a brief sketch of the architecture; you will need to design all the detail.

—SA
 
Share this answer
 
Comments
Maciej Los 26-Jun-12 18:55pm    
As always, well explained, my 5!
Sergey Alexandrovich Kryukov 26-Jun-12 19:04pm    
Thank you, Maciej.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900