Question for your brilliance...

Hi all.

Okay, that was a cheap attempt at flattery to entice you to read this post and (possibly) help ejimuk8 me. :)

I have an interesting situation which is about to turn into a huge research project for me, and I thought I'd try to gain the benefit of the combined genius of this group of bloggers.

I'm about to have to write a Windows Service to act as an interpreter between an old (30 years or so) suite of programs and a new .NET data-driven web application.  This service will basically impersonate the Server component of this old system, passing messages back and forth between the old sytem and the new system.

Apparently, the old system does some crazy magic where it can be simultaneously connected to multiple applications on the same port.  While I've done some socket programming, I'm not really an expert, so this sounds strange to me.  I always thought, once you connect via a port, that port gets “closed” to other connections.  Am I wrong here?  If not, how would you set up connections in .NET so that you can have multiple connections on the same port?

Comments

# re: Question for your brilliance...

Thursday, June 10, 2004 5:40 PM by Scott Allen

Every socket has four pieces of information to make it unique: server IP address, server port, client IP address, client port. If I look at netstat right now, I see IE opened two connections to your web server on port 80, one from port 4766, and one from port 4767. I have two connections to the same port on your server, but since the client ports are different the communication works. Likewise, I’m sure weblogs.asp.net has many connections opened on port 80, but each one will come from a different client ip / client port combination.

You can only listen on a port from one socket at a time, but the Accept method will return a new instance of the Socket class for each client.

Am I making any sense?

# re: Question for your brilliance...

Thursday, June 10, 2004 5:50 PM by Ken Robertson

One other thing to note is that the application has to be multithreaded in order to handle multiple connections. You can do something as dirty as accepting the connection and forking, then having the child process deal with the new connection and the parent process go back to waiting for a new connection. Or, you could use actual threads for better memory and process management. But basically, a single instance of the application running can only do one thing at a time, so if it is working with the new client, it can't be listening for new connections, so in order to support multiple connections, you always need to have one process listening.

As far as threading in .NET, I'm not sure. My experience in socket programming and threading is from the Unix realm.

# re: Question for your brilliance...

Thursday, June 10, 2004 8:03 PM by Steven M. Cohn

To further clarify Ken's idea: start with your main thread. The entire purpose of this main thread is to listen on the incoming port. As soon as a request arrives, fire off another worker thread to do process the request. This is typically a very fast an efficient action so the main thread can immediately get back to listening for further requests. If you can, keep your worker threads as autonomous as possible; interact with the main thread as little as possible. Depending on your bandwidth requirements, it may be necessary to increase process priority or, for very high throughput, move to an SMP system. There are plenty of examples out there on multithreading using .NET. Good luck.

# re: Question for your brilliance...

Friday, June 11, 2004 8:50 AM by Chris McKenzie

Thanks guys. That was exactly the information I'm looking for. I just ordered the multi-threading book, so by the time I need this to work with multiple connections, I'll have it. Now at least I know that what I need to do can be done--and as NBC so often reminds us <grin> "Knowing is half the battle!"

Leave a Comment

(required) 
(required) 
(optional)
(required)