By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,977 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

Sockets: Multiple Clients, One port

P: n/a
From: "Factor" <ja**********@gmail.com>
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.

Apr 19 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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]

<ja**********@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
| From: "Factor" <ja**********@gmail.com>
| 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.
|
Apr 19 '06 #2

P: n/a
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

Apr 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.