Multiple listeners on a TCP/IP port?

P

Paul A. Steckler

I need to write a TCP/IP server in C# that can handle multiple connections.
My first try was to use TCPListener instances in multiple .NET threads.
Of course, I got an exception from System.Net.Sockets about multiple
sockets on the same port. This happens even with a single listener in
multiple
Win32 processes.

Will I get better results by using a Socket instance with BeginAccept?
Should I run multiple threads, or do the asynchronous callbacks already
give me enough concurrency?

-- Paul
 
T

Tom Shelton

I need to write a TCP/IP server in C# that can handle multiple connections.
My first try was to use TCPListener instances in multiple .NET threads.
Of course, I got an exception from System.Net.Sockets about multiple
sockets on the same port. This happens even with a single listener in
multiple
Win32 processes.

Will I get better results by using a Socket instance with BeginAccept?
Should I run multiple threads, or do the asynchronous callbacks already
give me enough concurrency?

-- Paul

My personal preference is to use a Socket instance and the async methods
- but, your problem is caused by a misuse of the TCPListener class. You
only need one instace of the TCPListener class. You accept connections
using ether the AcceptTcpClient or AcceptSocket methods. What get's
returned from those methods is a new socket or tcpclient object that
then will act as your communications channel.

You may want to have a look here for some information on sockets in
..NET:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconsockets.asp

Anyway, there are some code examples, etc. That is of course, not a
complete reference on network programming, but it maybe a useful
starting point.
 
P

Paul A. Steckler

Tom Shelton said:
My personal preference is to use a Socket instance and the async methods
- but, your problem is caused by a misuse of the TCPListener class. You
only need one instace of the TCPListener class. You accept connections
using ether the AcceptTcpClient or AcceptSocket methods.

Oops, right.

I've got my code working fine now, thanks.

-- Paul
 

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