c# - Help with event driven TCP server -


I am working on an "application system", where I also need to create a server application. .NET 4.0). The server will mainly collect data from various POS applications / customers (which should be approximately 50-100, but the server should be able to handle 200-300 customers too). A server from a customer will probably get 100x times per day around 1 KB. The server must primarily accept the data, decrypt it, and store it on disk.

I am quite new to C # and server programming, please stand with me, it is also sending new configurations to customers to check changes in specific directories. I thought about using ThreadPulling and Asynp methods (there is a good example in a book "In a Brief" in C #) but I spend some time looking for the best solution and I have found this. But multithreading brings more problems than profit in my case. In this way I even thought about the operated server. "A single process can handle every incident on the callback (acceptable connection, available data for reading, can write to the customer ...)." "" I know that my problem is the best solution.

But I do not know how to code, I could not find an example about the event-driven server. As far as I understand that I should make a thread (1 for GUI), then create a TCP listener and then create events in any way so that when the TCP listener can accept the customer, it will be available for reading from the customer,

Please help me out of this code, I'm completely lost. I know how I can do it.

  while (true) { Check whether the client can connect to the customer Wants to hack and wants to iterate in the client list through the client list and check whether any data is being sent. Accept the data and store it ...}   

But that event is not running and the CPU is wasting. The server will not be very active, so I want to make it as efficient as possible.

Some examples will really help.

Thank you for your time and answer.

PS Can I use only one port for all customers?

Edit: To clarify, I want to code an event-driven server, but I do not know how, thus I just know who I am (customer Voting) has made an example.

Here is a server that may require skeleton for you. No exception is handled.

  class program {public static manual reset reset connect = new manual reset event (wrong); Static zero main (string [] args) {string ip = "127.0.0.1"; Int port = 14500; TcpListener Server = New TcpListener (IPAddress.Parse (IP), Port); Server.Start (); Console.witline ("server started ..."); While (true) {connected.Reset (); Server.BeginAcceptTcpClient (new asyncCallback (AcceptCallback), server); Connected.WaitOne (); }} Public Static Zero AcceptCallback (IAsyncResult AR) {TcpListener Listener = (TcpListener) ar.AsyncState; TCP Client Client = Listener. IndicPTCP Client (AR); Byte [] buffer = new byte [1024]; NetworkStream ns = client.GetStream (); If (nscore read) {nsbearriere read (buffer, 0, buffer length, new async callback (read callback), new object [] {ns, buffer}); } Connect .set (); } Public static zero ReadCallback (IAsyncResult AR) {NetworkStream ns = (NetworkStream) ((as in ar.AynyncState object []) [0]); Byte [] buffer = (byte []) (([ar] as an annyncate object []) [1]); Int n = ns.EndRead (AR); If (n> gt; 0) {Console.WriteLine (encoding. ASCII.GetString (buffer, 0, n)); } Ns.BeginRead (buffer, 0, buffer length, new async callback (readbackback), new object [] {ns, buffer}); }}    

Comments