473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.

Dec 25 '06 #1
4 10125
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vlad | last post by:
Is there any way to install multiple instances of the same windows service designed with VS.NET 2003? I tried copying the binaries into a separate folder and then copying registry entries for the...
1
by: Jack David | last post by:
I need direction on how to create a program that will start multiple windows services. Each service will monitor a specific directory using FileSystemWatcher. I have the file system watcher part...
0
by: Sunil Pandita | last post by:
Hi all, I have created a windows service that is working well. Now i want to create the multiple instances of this service on the same box. How could it be possible?. One way that I thought...
7
by: Giulio Petrucci | last post by:
Hi everybody, I've a big problem with a Windows Service I've created using C#. It's a very simple service: it just starts a TcpListener listening for incoming connections on a certain ports....
3
by: D. Yates | last post by:
Hi, I'm about to embark on a project that will both send and receive information to/from our client computers in the field. The area that I still need to finalize is the method of...
2
by: Jack | last post by:
Hi c++ guru's I need your help. I have 4 machines and I need them to be able to communicate to each other via peer to peer architecture. I know that when we have two machines one machine acts as...
1
by: jigsmshah | last post by:
how can i create one more instance of windows service on the same sysyem? I have one service already working,the new requiremnt is to create one more instance of the same service on the same system....
0
by: jigsmshah | last post by:
I have a windows service developed in C#.I am reading the connection string from an ini file and also i am reading 3 image file from the bin directory. now the new requirement is that there will be...
0
by: pbd22 | last post by:
Hi. I am wondering how to handle AbsoluteUri values inside windows services? It seems that the absoluteuri is called from the System.Web.UI.Page namespace which is not allowed inside a windows...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.