By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

How to handle multiple incoming TCP connections in Windows Services

P: n/a
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.

Dec 25 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Dec 26 '06 #2

P: n/a
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
<sr*******@gmail.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
>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.

Dec 26 '06 #3

P: n/a
Thanks Ignacio. The queue solution worked.

Ignacio Machin ( .NET/ C# MVP ) wrote:
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
<sr*******@gmail.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
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.
Dec 26 '06 #4

P: n/a
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.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins

<sr*******@gmail.comwrote in message
news:11**********************@42g2000cwt.googlegro ups.com...
Thanks Ignacio. The queue solution worked.

Ignacio Machin ( .NET/ C# MVP ) wrote:
>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
<sr*******@gmail.comwrote in message
news:11**********************@73g2000cwn.googlegr oups.com...
>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.

Dec 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.