473,696 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sockets: Multiple Clients, One port

From: "Factor" <ja**********@g mail.com>
Newsgroups: microsoft.publi c.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
2 7117
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(IPAd dress.Any, 9001);
TcpListener listener = new TcpListener(loc alEP);
listener.Start( );
TcpClient client = null;
while(started)
{
client = listener.Accept TcpClient();
client.ReceiveT imeout = 10000; // Wait 10 sec for data.
ClientReader(cl ient); // 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**********@g mail.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
| From: "Factor" <ja**********@g mail.com>
| Newsgroups: microsoft.publi c.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2201
by: Bonj | last post by:
I've been following a socket programming tutorial to make a simple TCP communication program, seemingly without hitches, it appears to work fine. However the structure of it is to have a server listening for requests from a client using listen(), and when one connects, it communicates with that client but only that one. It doesn't listen for more requests. What I'm wondering is can I have a server that continually listens for requests...
4
2165
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I create a socket, bind it to a port, put it in listen mode, and then n sockets can connect to it. The code: Socket newSocket = listeningSocket.Accept(); returns a socket. I can communicate on newSocket, and listeningSocket goes
2
1430
by: ZorpiedoMan | last post by:
I'm new to the world of sockets, and this question is not VB specific: If multiple clients access the same server on the same port, and the server is set up to do some async communication, does the server's response back to all the clients on that port, or just to the one who sent the request? In other words: Client One - Request Data From Server (It takes a few seconds for the server to get the answer)
2
1230
by: ZorpiedoMan | last post by:
I'm new to the world of sockets, and this question is not VB specific: If multiple clients access the same server on the same port, and the server is set up to do some async communication, does the server's response GO back to all the clients on that port, or just to the one who sent the request? In other words: Client One - Request Data From Server (It takes a few seconds for the server
0
1089
by: altu | last post by:
Hello ! All I want to create a server program where Server listens to a specific port take it 8000 there would be many clients who may need to connect to this port same time even when other client has connection eastiblished there , So i created a small vb.net listner from the MSDN sample where i observed the client binds itself to a different port once and port 80000 is left in Listening mode for other clients which can be seen using...
7
1422
by: Steven | last post by:
Hi, Thanks all for you help with my "socket and buffer size" question. I have decided to use async ones, and my code is working pretty well. But i still have a non-answered question: Is it a problem if all the client are connected and sending a large quantity of bytes simultaneously to the same port of the server ?
14
11899
by: eliss.carmine | last post by:
I'm using TCP/IP to send a Bitmap object over Sockets. This is my first time using C# at all so I don't know if this is the "right" way to do it. I've already found out several times the way I was doing something was really inefficient and could reduce 10 lines of code with 2, etc. For reading, I am using a TcpClient and I call NetworkStream ns = client.GetStream(); to get a stream stream.Read(buffer, 0, buffer.Length);
1
3630
by: jcprince | last post by:
Hi Not sure I can do what I'm trying to do without using a 3rd party component like Dart. I need to build a windows service to create a socket connection on an IBM mainframe using an IP and port combination. No problem there. However, due to the expected volume (at least 20x10K streams per second in each direction), the mainframe sysadmin has requested the service use multiple 'conversations' within the single IP connection. The...
0
8667
marktang
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9148
Oralloy
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...
0
9012
jinu1996
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...
1
8880
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,...
0
8853
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6515
isladogs
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...
0
5857
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();...
0
4611
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1992
bsmnconsultancy
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.