Re: How to handle multiple incoming TCP connections in Windows Services



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

--
Ignacio Machin
machin AT laceupsolutions com


<sracherla@xxxxxxxxx> wrote in message
news:1167080746.216521.151080@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.



.



Relevant Pages

  • Re: How to handle multiple incoming TCP connections in Windows Services
    ... Ignacio Machin wrote: ... inserting the connections in a queue and then spawning a new thread ... I need the service to accept multiple incoming requests. ... TcpClient tcpClient = new TcpClient; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to handle multiple incoming TCP connections in Windows Services
    ... you have a thread just receiving connections in a loop and ... connection to stay alive until the client closes it. ... I need the service to accept multiple incoming requests. ... TcpClient tcpClient = new TcpClient; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [take25 1/6] kevent: Description.
    ... To ensure every connection is handled as quickly as possible you stuff them all in the same queue and then have all threads use this one queue. ... In nscd, for instance, we have one single thread waiting for incoming connections and it then has to wake up a worker thread to handle the processing. ... With the new event handling this wouldn't be the case, one thread only is woken and we don't have to wake worker threads. ... I think I already gave my opinion on a ring buffer, ...
    (Linux-Kernel)
  • Re: Bug? Kernels 2.6.2x drops TCP packets over wireless (independentof card used)
    ... TCP connections hang and timeout before all data is read. ... ICMP and UDP protocols seem to work (provided by 0% packet loss ping to router (and internet servers), ... wifi0: Use hw queue 1 for WME_AC_BE traffic ...
    (Linux-Kernel)
  • Re: Exchange dies weekly
    ... In fact, in some rare cases, if you wait for it to lock up, it is so frozen you can't even view queues anymore. ... unless you *just* restarted (which will obviously flush all connections) you can check this anytime. ... There is a known bug *IF* you use SenderID filtering that causes Exchange to see connections in an open state even after the remote server has closed the connection. ... This results in your queue slowly filling up and eventually running out of resources and hanging. ...
    (microsoft.public.windows.server.sbs)