How to handle multiple incoming TCP connections in Windows Services



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: Controlling Javascript from server side
    ... but five different language implementations here. ... 'true' means that the request must be handled asynchronously. ... There is exactly *no* reason for such a thing here. ... | percent-endoded string). ...
    (comp.lang.javascript)
  • Re: Is it ok to change $ENV{QUERY_STRING} before "use CGI;" is called..?
    ... the big advantage of wiki, ... It does url-encode if its option is set to use UTF-8 request, ... string for GET requests. ... because "query string" in URL is anyway just a string ...
    (comp.lang.perl.misc)
  • Re: Problems installing Microsoft .NET Framework 1.1 Service Pack
    ... and it worked and I have filled in the request and sent it. ... An unhandled exception occurred during the execution of the ... siteName, String httpVerb, String path, String QS, String httpVersion, ... request, String site, TicketState& ticketState, String& responseHeaders) +100 ...
    (microsoft.public.windowsupdate)
  • Re: Problems installing Microsoft .NET Framework 1.1 Service Pack
    ... and it worked and I have filled in the request and sent it. ... An unhandled exception occurred during the execution of the ... siteName, String httpVerb, String path, String QS, String httpVersion, ... request, String site, TicketState& ticketState, String& responseHeaders) ...
    (microsoft.public.windowsupdate)
  • Re: which pattern to use...
    ... Pick from queue. ... readup and it says pass it as string, then parse the string in the ... Then Request gets the next module, checks its type, ... Module.processwith the data packet object. ...
    (comp.object)

Quantcast