Note that there are some explanatory texts on larger screens.

plurals
  1. POWinsock2 tcp/ip - some data packets are ignored probably due to null terminator from the previous packet
    primarykey
    data
    text
    <p>I wrote a simple client-server program. Network.h is a header file which uses Winsock2.h (TCP/IP mode) to create socket, accept/connect in blocking mode, send/recv in non-blocking mode. I made it so that the function <code>string TNetwork::Recv(int size)</code> will return the string "Nothing" if it gets WSAWOULDBLOCK error (no data is received yet)</p> <p>Here is my main function:</p> <pre><code>int main(){ string Ans; TNetwork::StartUp(); //WSA start up, etc cin &gt;&gt; Ans; if (Ans == "0"){ // 0 --&gt; server TNetwork::SetupAsServer(); //accept connection (in blocking mode!) while (true){ TNetwork::Send("\nAss" + '\0'); //without null terminator, the client may read extra bytes, causing undefined behavior (?) TNetwork::Send("embly" + '\0'); cin &gt;&gt; Ans; } } else{ // others --&gt; regard Ans as IP address. e.g. I can type "127.0.0.1" TNetwork::SetupAsClient(Ans); string Rec; while (true){ Rec = TNetwork::Recv(1000); if (Rec != "Nothing"){ cout &lt;&lt; Rec; } } } system("PAUSE"); } </code></pre> <p>Supposedly, the client would print "Assembly" when connected, and when the server enters anything to its console window. Sometimes, though, the client would only print out "\nAss" in the console without the "embly.</p> <p>To my understanding, TCP/IP ensures all data to be sent and in the correct order, so I guess what happens is that both packets arrive at the same time, which happen quite often over the unstable internet. And due to this null terminator, the client would ignore the "embly", since the Recv() function stopped reading when it hits a null terminator.</p> <p>So, how can I ensure that the client will always read all data packets correctly?</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.
 

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