Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the current standard of practice to implement an async socket client?
    text
    copied!<p>I look for the best practices to meet the following requirements:</p> <ul> <li>A framework that handles several client sockets in an asynchronous fashion</li> <li>Each incoming message protocol dictates that each message is of format string and is marked complete with a line feed character '\n'. </li> <li>I have full control of the client side but not server side. Server accepts and sends string based messages with line feed character to mark completion of the message.</li> <li>I want to be able to send messages through each connected socket at any given time (each socket can receive and send messages).</li> <li>Incoming messages should be forwarded through a callback. </li> <li>I want to be able to chose in my implementation whether all incoming complete messages from all connected sockets are routed to one single callback or whether each socket client implements its own callback. </li> <li>I connect at a maximum 4 clients/sockets. So I look for suggestions that capitalize on such limited amounts of sockets, however, are capable of managing all those concurrently. </li> </ul> <p>I wonder whether the framework where I use <code>BeginReceive</code> and <code>EndReceive</code> with implemented IAsyncResult callback is state of the art given I target .Net 4.5. Is there a better solution, such as using NetworkStream or other API choices? What really bugs me with the BeginReceive/EndReceive implementation is that after EndReceive I have to call BeginReceive again and register the callback again. That sounds like an awful amount of overhead to me. Why can't new data be added async at any time and at the same time another context builds complete messages that are then routed through a raised event? </p> <p>The argument of using IAsyncResult is often given in that thread handling is taken care of, but what speaks against the following: Using a NetworkStream and simply read from and write to the stream. As mentioned only string messages are exchanged and each message per protocol is marked complete by the line feed character. A separate task/thread would poll a streamreader (based on the networkstream) through <code>ReadLine()</code>. It probably can`t get simpler than that, can it?</p> <p>What I am basically asking is, how can the following code be made truly async? </p> <pre><code>public class SocketClient { private TcpClient client; private StreamReader reader; private StreamWriter writer; public event Action&lt;string&gt; MessageCallback; public SocketClient(string hostname, int port) { client = new TcpClient(hostname, port); try { Stream stream = client.GetStream(); reader = new StreamReader(stream); writer = new StreamWriter(stream); writer.AutoFlush = true; //Start Listener on port StartListener(); } catch (Exception e) { throw new Exception(e.ToString()); } } public void StartListener() { Task task = Task.Factory.StartNew(() =&gt; { while (true) { if (MessageCallback != null) { MessageCallback(reader.ReadLine()); } //Thread.Sleep(200); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload