c++ - Garbage values and Buffers differences in TCP -


first question: confused between buffers in tcp. trying explain proble, read documentation tcp buffer, author said lot tcp buffer, thats fine , explanation beginner. need know tcp buffer same buffer 1 use in our basic client server program (char *buffer[some_size]) or different buffer hold tcp internally ?

my second question sending string data prefix length (this data me) client on socket server, when print data @ console along string prints garbage value "this data me zzzzzz 1/2 1/2....." ?. fixed right shifting char *recvbuf = new char[nlength>>3]; nlength 3 bits why need in way ?

my third question in relevance first question if there nothing tcp buffer , char *buffer[some_size] whats difference program notice using such static memory allocation buffer , using dynamic memory allocation buffer using char *recvbuf = new char[nlength];. in short best , why ?

client code

int bytessent; int bytesrecv = socket_error; char sendbuf[200] = "this data me";  int  nbytes = 200, nleft, idx; nleft = nbytes; idx = 0; uint32_t varsize = strlen (sendbuf); bytessent = send(connectsocket,(char*)&varsize, 4, 0); assert (bytessent == sizeof (uint32_t)); std::cout<<"length information in:"<<bytessent<<"bytes"<<std::endl; // code make sure  data has been sent   while (nleft > 0) {     bytessent = send(connectsocket, &sendbuf[idx], nleft, 0);     if (bytessent == socket_error)     {       std::cerr<<"send() error: " << wsagetlasterror() <<std::endl;       break;     }     nleft -= bytessent;     idx += bytessent; }   std::cout<<"client: bytes sent:"<< bytessent; 

server code:

int bytessent; char sendbuf[200] = "this string test data server"; int   bytesrecv; int idx = 0; uint32_t  nlength; int length_received = recv(m_socket,(char*)&nlength, 4, 0);//data length info char *recvbuf = new char[nlength];//dynamic memory allocation based on data length info //code make sure data has been received while (nlength > 0) {     bytesrecv = recv(m_socket, &recvbuf[idx], nlength, 0);      if (bytesrecv == socket_error)     {         std::cerr<<"recv() error: " << wsagetlasterror() <<std::endl;         break;      }     idx += bytesrecv;     nlength -= bytesrecv; }    cout<<"server: received complete data is:"<< recvbuf<<std::endl;   cout<<"server: received bytes are"<<bytesrecv<<std::endl;   wsacleanup();   system("pause");   delete[] recvbuf;    return 0; 

}

first question: confused between buffers in tcp. trying explain proble, read documentation tcp buffer, author said lot tcp buffer, thats fine , explanation beginner. need know tcp buffer same buffer 1 use in our basic client server program (char *buffer[some_size]) or different buffer hold tcp internally ?

when call send(), tcp stack copy of bytes out of char array in-kernel buffer, , send() return number of bytes copied. tcp stack handle transmission of in-kernel bytes destination across network can. it's important note send()'s return value not guaranteed same number of bytes specified in length argument passed it; less. it's important note sends()'s return value not imply that many bytes have arrived @ receiving program; rather indicates number of bytes kernel has accepted , try deliver.

likewise, recv() merely copies bytes in-kernel buffer array specify, , drops them in-kernel buffer. again, number of bytes copied may less number asked for, , different number of bytes passed sender on particular call of send(). (e.g if sender called send() , send() returned 1000, might result in calling recv() twice , having recv() return 500 each time, or recv() might return 250 4 times, or (1, 990, 9), or other combination can think of adds 1000)

my second question sending string data prefix length (this data me) client on socket server, when print data @ console along string prints garbage value "this data me zzzzzz 1/2 1/2....." ?. fixed right shifting char *recvbuf = new char[nlength>>3]; nlength 3 bits why need in way ?

like joachim said, happens because c strings depend on presence of nul-terminator byte (i.e. 0 byte) indicate end. receiving strlen(sendbuf) bytes, , value returned strlen() not include nul byte. when receiver's string-printing routine tries print string, keeps printing until if finds nul byte (by chance) somewhere later on in memory; in meantime, see random bytes in memory before point. fix problem, either increase sent-bytes counter (strlen(sendbuf)+1), nul terminator byte gets received well, or alternatively have receiver manually place nul byte @ end of string after has received of bytes of string. either way acceptable (the latter way might preferable way receiver isn't depending on sender right thing).

note if sender going send 200 bytes rather number of bytes in string, receiver need receive 200 bytes if wants receive more 1 block; otherwise when tries receive next block first bytes (after string) before gets next block's send-length field.

my third question in relevance first question if there nothing tcp buffer , char *buffer[some_size] whats difference program notice using such static memory allocation buffer , using dynamic memory allocation buffer using char *recvbuf = new char[nlength];. in short best , why ?

in terms of performance, makes no difference @ all. send() , receive() don't care bit whether pointers pass them point @ heap or stack.

in terms of design, there tradeoffs: if use new, there chance can leak memory if don't call delete[] when you're done buffer. (this can particularly happen when exceptions thrown, or when error paths taken). placing buffer on stack, on other hand, guaranteed not leak memory, amount of space available on stack finite huge array cause program run out of stack space , crash. in case, single 200-byte array on stack no problem, that's use.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -