Message variable over TCP socket protocol in C ++

I tried to send the length prefix data to the server, I tried more to use the code and solution sent by other people when the stack overflowed. Still looking at how things work using TCP. How I don’t know so much (about network programming, but I know about theoretical concepts). I am writing what I have tried so far. Based on this, I have some questions.

Since we use Char Buffer[200] = "This is data"the client side to send (using the function send()) and char enter the data on the server (receiving using the function recv()). So far this is normal, but what if I need to send a variable-length message with length information ?, How can I encode the message length information?

for example:  0C     54 68 69 73 20 69 73 20 64 61 74 61     07    46 72 6f 6d 20 6d 65
           (length)  T  h   i  s    i   s     d  a  t  a   (length) F  r  o  m     m  e


How can i interpret these two message seperately from tcp stream  at the sever side ?

I do not know how the length of information can be sent separately. Or if someone can understand my test case (given below in the wording) to check the string length information.

EDIT: Everything seems to be fine, but I just need to check the length of the prefix a bit. I send 20 bytes to the server ("This is data from me"). My size of the received length is 4 bytes (I don’t know what is inside, I need to check that 4 bytes of the length that I received contain 0000 0000 0000 0000 0000 00000 0001 0100). Thats it, as I thought, to check it by changing the length of the information to 2 bits right now, it should look like (the 4 bytes of the length I received contain 0000 0000 0000 0000 0000 00000 0000 0101), in this case I have to get only 5 characters i.e. "It". Do you know how I can test this on the server side?

Client code

int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[200] = "This is data From 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 is in:"<<bytesSent<<"bytes"<<std::endl;
// code to make sure  all 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;
}
bytesSent = send(ConnectSocket, sendbuf, strlen(sendbuf), 0);
 printf("Client: Bytes sent: %ld\n", bytesSent);

Server code

     uint32_t  nlength;
int length_received = recv(m_socket,(char*)&nlength, 4, 0);
char *recvbuf = new char[nlength];
int byte_recived = recv(m_socket, recvbuf, nlength, 0);

thanks

+4
source share
1

, .

, , :

while (nLeft > 0)
{
    bytesSent = send(ConnectSocket, &sendbuf[idx], nLeft, 0);
    // [...]
}
bytesSent = send(ConnectSocket, sendbuf, strlen(sendbuf), 0);

, . ? , , .

, :

const uint32_t varSize = strlen (sendbuf);
bytesSent = send(ConnectSocket, &varSize, sizeof (varSize), 0);
assert (bytesSent == sizeof (uint32_t));
while (nLeft > 0)
{
    bytesSent = send(ConnectSocket, &sendbuf[idx], nLeft, 0);
    // [...]
}

. . (, 20 ) \0. , , , . , , .

+5

Source: https://habr.com/ru/post/1525431/


All Articles