How to handle multiple incoming TCP connections in Windows Services

S

sracherla

I am trying to write a simple windows service that accepts an incoming
request; receives a string input and sends a string output. I need this
connection to stay alive until the client closes it.
Also, I need the service to accept multiple incoming requests.

Here is the code:

public void Listen()
{
#region Commented Code
try
{
int portNumber = 500;
log.WriteEntry(this.ServiceName + ": Listening on port " + portNumber
+ " ...");

//TcpListener listener = new TcpListener(localEndPoint);

IPAddress localAddress = IPAddress.Parse("127.0.0.1");

TcpListener listener = new TcpListener(localAddress, 500);

listener.Start();
do
{
if (!pause)
{
#region TcpClient Method
TcpClient tcpClient = new TcpClient();

tcpClient = listener.AcceptTcpClient();

if (tcpClient != null)
{
log.WriteEntry("Connected to someone.");

string message = Receive(tcpClient);

log.WriteEntry("Received following message on " +
DateTime.Now.ToString() + " :" + message);

message = "Booya";

Send(tcpClient, message);

log.WriteEntry("Sent following message on " +
DateTime.Now.ToString() + " :" + message);
}
}
} while (canStopListening);
listener.Stop();
}
catch
{
throw;
}
#endregion

}

The code I have above works for one incoming request only. And the
service does not respond to any other futher incoming requests.

Any help will be greatly appreciated.
 
S

salman

you have to perform service to client in separate thread when client
connects, after accept you may create any thread;
tcpClient = listener.AcceptTcpClient();
after this line of code you have to call another function or create a
thread and perform the desire operation in other thread.

Salman
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Its very easy, you have a thread just receiving connections in a loop and
inserting the connections in a queue and then spawning a new thread

while(true)
{
Socket s = listener1.AcceptSocket();
syncedQueue.Enqueue( s );
new Thread( new ThreadStart( workerMethod) ).Start();
}

workerMethod()
{
Socket s = syncedQueue.Dequeue();
}

You have to use a synced queue though:

syncedqueue = Queue.Synchonize( new Queue() );


Pd:
all the above code was written here without checking msdn so it may not
compile
 
S

sracherla

Thanks Ignacio. The queue solution worked.
Hi,

Its very easy, you have a thread just receiving connections in a loop and
inserting the connections in a queue and then spawning a new thread

while(true)
{
Socket s = listener1.AcceptSocket();
syncedQueue.Enqueue( s );
new Thread( new ThreadStart( workerMethod) ).Start();
}

workerMethod()
{
Socket s = syncedQueue.Dequeue();
}

You have to use a synced queue though:

syncedqueue = Queue.Synchonize( new Queue() );


Pd:
all the above code was written here without checking msdn so it may not
compile
 
C

Chris Mullins

This solution, while working, has some inherient limitations.

It's using the "1 thread per client" algorithm for connections, which
doesn't scale very well. As long as you plan to connect a dozen or so
clients, you're good. If you plan to connect a few hundred, you're in
trouble. If you plan on connecting a few thousand, it's not going to work at
all.

Asynchronous sockets are the most scalable way to do this, and there's a ton
written about them all over the web.
 

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