Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I had a similar issue.</p> <p>I was sending JSON strings using the Socket class, and suddenly JSON.NET would throw an exception. I inspected the string, and I saw that 2 JSON (root) objects were being processed. I then threw together a minimal example, and found that the socket will stash* any unread data (unread = no .Receive has been called yet, and .Receive will clear the stash).</p> <p>*stash: I am not sure if this is the correct term. It made sense to me.</p> <p>Here is my test. If you want to run it, create a new test class and install XUnit.</p> <pre><code>/// &lt;summary&gt; /// Testing Net Sockets.. /// &lt;/summary&gt; /// &lt;author&gt;Jeff Hansen&lt;/author&gt; public class SocketTests { /// &lt;summary&gt; /// The local endpoint for the server. There's no place like 127.0.0.1 /// &lt;/summary&gt; private IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1337); /// &lt;summary&gt; /// Tests that a call to .Receive will return all unread data. /// &lt;/summary&gt; /// &lt;author&gt;Jeff Hansen&lt;/author&gt; [Fact] public void ReceiveWillReadAllUnreadData() { // We expect to receive double data. const string expected = "HELLOHELLO"; const string dataToSend = "HELLO"; // Start the server in a background task. // We do this because the receive call is blocking. var bgTask = Task.Factory.StartNew(() =&gt; { var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { // Bind to the endpoint and start listening. server.Bind(endpoint); server.Listen(10); // 10 is max allowed connections in queue (I think) // Client connected, receive data var connectingClient = server.Accept(); // So low-level we even get to allocate room for the data buffer manually. var buf = new byte[1024]; // Hangs until data flows. var sz = connectingClient.Receive(buf); // This is the size of the data we just received. var data = Encoding.ASCII.GetString(buf, 0, sz); // We use the size to only grab what we need from the buffer. Console.WriteLine("Data received: {0}", data); // This is going to pass because we sent the data twice, // and the call to Receive would not be able to complete in time // for it to clear before more data becomes available. Assert.Equal(expected, data); /* * BAM! Theory proven. We seriously had issues * because we didn't understand how it worked. This is why you usually end * your transmission with a newline. */ Console.WriteLine("Server closing"); server.Close(); } catch (Exception e) { // Make sure we close the server. server.Close(); throw; } }); // Create a client socket and connect it to the server. // The server thread should have started it up by now. var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(endpoint); // Get the bytes of our string var buffer = Encoding.ASCII.GetBytes(dataToSend); // Send it out twice. This happens faster than the server will process it, // so the data is stacking up. You would THINK that .Receive would // simply return the first data sent to it, and the next time .Receive is called // return the next. But that's not how it works, apparently. client.Send(buffer); client.Send(buffer); // Wait for the server to finish whatever it's doing. try { // We give it 3000ms to complete. bgTask.Wait(); } catch (AggregateException ag) { // Throw any esceptions that were thrown in the background thread. ag.Handle(ex =&gt; { throw ex; }); } // Close the client socket. client.Close(); } } </code></pre> <p>We solved it by delimiting our data with a newline.</p> <p>Fun fact: I was messing with TcpClient, and found that if I did not write a newline to the stream, it would not send anything - it would just hang there. Might have been a localized issue though. Anyone care to elaborate on that?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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