Sockets: Multiple Clients, One port

J

jasonsgeiger

From: "Factor" <[email protected]>
Newsgroups: microsoft.public.in.csharp
Subject: Multiple Clients, One port
Date: Wed, 19 Apr 2006 09:36:02 -0700

I'm been working with sockets for a short while now using a server
program a former coworker started. The program listens on a port for
incomming connections. When a valid connection is made (we send this
init string into the socket from the clients) the server closes the
socket so it can start listening on that port again for other clients
and starts a new thread that contacts the client back on some random
outgoing server port. Maybe this seems stupid, but at the time we
thought there was no way to have multiple connections on one port at a
time.

Then we started thinking about it. FTP, SMTP. These all operate on
standard ports and handle multiple simultanious clients/connections.
How is this done? My curiosity never peek enough to actually try to
code it.

Fortunately/Unfortunately, the time has come for me to impliment
something like this - not because I'm just curious - but because the
clients are soon be heavily firewalled and I will not be able to have
the server "contact them back" like we are doing now. I have to figure
out how to get the clients to log into this port on the server, and not
block out all other clients while they are communicating. Sessions are
often only a few seconds long but they CAN be up to an hour (we're
doing work over the cellular network, it's very slow and roundtrip
latency cane be up to two seconds! Yikes!) If I have a client logged
in for 45 minutes it cannot be blocking that port for all other clients
for that time.

So to my question: Am I attempting the impossible? How do I go about
doing this? Does anyone know of any resources that I can read online
or purchase(books, CDs) to help me get started with this multiple
clients on one port application? Am I incorrect about thinking that
one port can handle more than one connection (if so, then how does
SMTP, POP3, FTP, or any other common port server function)?

I've found very little out there on the internet... anyone have any
sugestions?
I can post cost if necessary, but since I'm really looking for help on
new nonexistant code, then I'm not sure what good it would do anyone.

I thank you all ahead of time for any help I receive. Thank you for you
time,

Jason

P.S. I know I can come up with some scheme that the client logs in and
then i send it back a port number from a known pool on my server. Then
the client can log out and then log back into that port. This seems
Sockets: messy and unnecessary. It'd rather try to make it work all on
one port.
 
W

William Stacey [MVP]

You "listen" on one socket and accept *new client sockets. You don't close
the listen socket until your server is closed or you want to stop listening
for new connections. You can work with the new client socket on another
thread - that is sometimes called a threaded server or thread-per-client
server.
You could also write an async server that does not spin another thread per
client, but gets a callback when some data is found on the socket. These
are harder to write and reason about, but scale better. If you will have
more then say 1000 concurrent clients, then async is pretty much required.
Here is a (from memory) sample tcp server snippet:

private bool started = true;
private void ListenLoop()
{
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9001);
TcpListener listener = new TcpListener(localEP);
listener.Start();
TcpClient client = null;
while(started)
{
client = listener.AcceptTcpClient();
client.ReceiveTimeout = 10000; // Wait 10 sec for data.
ClientReader(client); // Starts a new
thread and handles dialog with client on that thread.
}
listener.Stop();
}

That is the general idea. The devil is in the details. Server class needs
to be thread safe for any state that is cross-thread, such as "started" var,
etc.

--
William Stacey [MVP]

| From: "Factor" <[email protected]>
| Newsgroups: microsoft.public.in.csharp
| Subject: Multiple Clients, One port
| Date: Wed, 19 Apr 2006 09:36:02 -0700
|
| I'm been working with sockets for a short while now using a server
| program a former coworker started. The program listens on a port for
| incomming connections. When a valid connection is made (we send this
| init string into the socket from the clients) the server closes the
| socket so it can start listening on that port again for other clients
| and starts a new thread that contacts the client back on some random
| outgoing server port. Maybe this seems stupid, but at the time we
| thought there was no way to have multiple connections on one port at a
| time.
|
| Then we started thinking about it. FTP, SMTP. These all operate on
| standard ports and handle multiple simultanious clients/connections.
| How is this done? My curiosity never peek enough to actually try to
| code it.
|
| Fortunately/Unfortunately, the time has come for me to impliment
| something like this - not because I'm just curious - but because the
| clients are soon be heavily firewalled and I will not be able to have
| the server "contact them back" like we are doing now. I have to figure
| out how to get the clients to log into this port on the server, and not
| block out all other clients while they are communicating. Sessions are
| often only a few seconds long but they CAN be up to an hour (we're
| doing work over the cellular network, it's very slow and roundtrip
| latency cane be up to two seconds! Yikes!) If I have a client logged
| in for 45 minutes it cannot be blocking that port for all other clients
| for that time.
|
| So to my question: Am I attempting the impossible? How do I go about
| doing this? Does anyone know of any resources that I can read online
| or purchase(books, CDs) to help me get started with this multiple
| clients on one port application? Am I incorrect about thinking that
| one port can handle more than one connection (if so, then how does
| SMTP, POP3, FTP, or any other common port server function)?
|
| I've found very little out there on the internet... anyone have any
| sugestions?
| I can post cost if necessary, but since I'm really looking for help on
| new nonexistant code, then I'm not sure what good it would do anyone.
|
| I thank you all ahead of time for any help I receive. Thank you for you
| time,
|
| Jason
|
| P.S. I know I can come up with some scheme that the client logs in and
| then i send it back a port number from a known pool on my server. Then
| the client can log out and then log back into that port. This seems
| Sockets: messy and unnecessary. It'd rather try to make it work all on
| one port.
|
 
C

Clint (cmueller

I used this link from MSDN Magazine to help me work with Socket
programming ... it might come in handy for you:

"Get Closer to the Wire with High-Performance Sockets in .NET"
http://tinyurl.com/rmqym

Clint
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top