I have a Windows service that runs some tasks. I need to add some networking functionality to its list of tasks.
Here is what the networking logic will look like:
+-------------------+ +---------------+
| | send request | |
| | for some data | |
| | <--------------------------------------------- | |
| | | |
| | process request | Client |
| Windows Service | | Application |
| | send response | |
| | ---------------------------------------------> | |
| | | |
| | process response | |
+-------------------+ +---------------+
NOTE: There can be multiple client applications requesting data simultaneously.
NOTE: Each client application can send multiple requests for data simultaneously.
NOTE: The Windows service and client application could reside within the same network or on separate networks.
Here's the basic layout of the Windows service:
class Processor : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer Timer1 = null;
private System.Timers.Timer Timer2 = null;
private System.Timers.Timer Timer3 = null;
protected override void OnStart(string[] args)
{
Timer1 = new System.Timers.Timer();
Timer1.Interval = ...;
Timer1.AutoReset = true;
Timer1.Elapsed += new ElapsedEventHandler(...);
Timer1.Start();
Timer2 = new System.Timers.Timer();
Timer2.Interval = ...;
Timer2.AutoReset = true;
Timer2.Elapsed += new ElapsedEventHandler(...);
Timer2.Start();
Timer3 = new System.Timers.Timer();
Timer3.Interval = ...;
Timer3.AutoReset = true;
Timer3.Elapsed += new ElapsedEventHandler(...);
Timer3.Start();
// I was thinking of creating a new Task (System.Threading.Tasks.Task) or Timer (System.Timers.Timer) here to handle the networking-related logic.
}
protected override void OnStop()
{
Timer1.Stop();
Timer1.Close();
Timer2.Stop();
Timer2.Close();
Timer3.Stop();
Timer3.Close();
}
}
I've been reading Microsoft's Network Programming documentation, but I still don't know which class would be best for my situation.
The classes I've been thinking of using are:
- Socket
- TcpListener (for the Windows service) & TcpClient (for the client application)
- HttpListener (for the Windows service) & HttpClient (for the client application)
The TcpListener and TcpClient classes are wrappers of the Socket class. They seem to hide many of the implementation details and make the code easier to understand. The Socket class seems quite overwhelming with the amount of functionality it provides, but it can handle multiple client connections. Can the TcpListener handle multiple clients or is it only able to listen for one at-a-time?
What networking class would be best for my situation? How can I guarantee the requests will be served in a responsive manner, while also guaranteeing the non-network related tasks will continue without being blocked?