Help with Event Driven TCP Server

I am working on an “application system” where I also need to create a server application. I work in C # (.NET 4.0). The server will mainly collect data from different POS applications / clients (which should be around 50-100, but the server should also be able to handle about 200-300 clients). From a single client, the server is likely to receive about 1 KB about 100 times a day. The server basically needs to receive data, decrypt it and store it on disk. He also needs to check for changes in a specific directory to send new configurations to clients, which should not be very common.

I am new to C # and server programming, so please bear with me. I was thinking about using threadpooling and async methods (there is a good example that is used in the book “C # in a nutshell”). But I spent quite a bit of time searching for a better solution, and I found this. But multithreading brings more problems than advantages in my case. So I was thinking about even a managed server. "One process that processes each event (received connection, readable data, can write to the client, ...) in the callback." from what is an event driven web server . I think this is the best solution to my problem.

But I have no idea how to encode it, I could not find any examples of event-driven servers. As I understand it, I have to make one thread (+ 1 for the GUI), then create a TCP listener, and then somehow create events so that when the TCP listener listens for the client, the event could start and wake up the server, as well as when the data read from clients was available, he would wake the server.

Please help me to code this, I am completely lost. I know how to do this using

while(true) { check if client wants to connect accept client and add it to client list iterate through client list and check if anyone is sending data ... accept data and store it ... } 

But this is not event related and wastes the processor. The server will not be very active, so I would like to make it as efficient as possible.

Some examples will really help.

Thanks for your time and answers.

ps Is it possible to use only one port for all clients?

EDIT: To clarify, I want to code an event-driven server, but I don't know how to do this, so I just made an example of what I know (client polling).

+6
c # event-driven tcp
source share
6 answers

Here is the server skeleton for what you might need. No exceptions are handled.

 class Program { public static ManualResetEvent connected = new ManualResetEvent(false); static void Main(string[] args) { string ip = "127.0.0.1"; int port = 14500; TcpListener server = new TcpListener(IPAddress.Parse(ip), port); server.Start(); Console.WriteLine("Server started..."); while (true) { connected.Reset(); server.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), server); connected.WaitOne(); } } public static void AcceptCallback(IAsyncResult ar) { TcpListener listener = (TcpListener)ar.AsyncState; TcpClient client = listener.EndAcceptTcpClient(ar); byte[] buffer = new byte[1024]; NetworkStream ns = client.GetStream(); if (ns.CanRead) { ns.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(ReadCallback), new object[] { ns, buffer }); } connected.Set(); } public static void ReadCallback(IAsyncResult ar) { NetworkStream ns = (NetworkStream)((ar.AsyncState as object[])[0]); byte[] buffer = (byte[])((ar.AsyncState as object[])[1]); int n = ns.EndRead(ar); if (n > 0) { Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, n)); } ns.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(ReadCallback), new object[] { ns, buffer }); } } 
0
source share

Firstly, if you are new to C # and multithreading and sockets, it is very difficult to bite off your first project. I recommend studying them individually.

However, you may find Nito.Async.Sockets useful; It includes an event driven server socket and handles multithreading issues for you.

+3
source share

I don't know about C # structures, but you can look at Twisted , an event-based Python structure. You can find sample server and client code similar to your needs.

Here is an example of a very simple server that sends back to the client everything that it received from it:

 #!/usr/bin/env python # Copyright (c) 2001-2009 Twisted Matrix Laboratories. # See LICENSE for details. from twisted.internet.protocol import Protocol, Factory from twisted.internet import reactor ### Protocol Implementation # This is just about the simplest possible protocol class Echo(Protocol): def dataReceived(self, data): """ As soon as any data is received, write it back. """ self.transport.write(data) def main(): f = Factory() f.protocol = Echo reactor.listenTCP(8000, f) reactor.run() if __name__ == '__main__': main() 

Regarding the last question:

PS Can I use only one port for all clients?

Yes you can (any language / frame you use).

+1
source share

I would suggest using WCF as a Windows service that provides a scalable and multi-threaded platform. It can also be adapted according to protocol requirements. Here is an example that can be used as a reference:

http://msdn.microsoft.com/en-us/library/ms733069.aspx

+1
source share

An event-based event does not exactly describe what you expect from your system, for example, because you are describing a survey of your customers for data processing, and not so that your customers push their data to your service (triggering an event).

If you have very few ideas on how to code such a system, you can explore existing solutions / products for your scenario.

I would recommend checking out EAI tools like BlueIntegrator or BizTalk for integrating POS clients.

For your requirement regarding updating client updates, you can check out BITS .

+1
source share

I'm a bit late to the party, but recently I started a new job developing software for integrating with scientific tools, and currently I'm experiencing pain in learning about threads, commits, asynchronous processing, etc. I found Joe Albahari Threading in C # to be a great resource for exploring the piercing side of things.

0
source share

All Articles