Stelios Savva
Stelios Savva

Reputation: 832

Add a header to a string and sending it with TCP socket

I have a client/server that exchanges messages, and i am trying to add the size of the string i am sending, in the beginning of the string, in order for the server to know how many bytes to read. I added the message starting from the +4 pos of the char* and used memcpy to copy the strlen of the string. It doesnt seem to work and something tells me its the wrong way to do it. This is my code.

//*CLIENT*//
send_message = malloc(1024 * sizeof(char));
strcpy(send_message + 4,"GETFILES ");
strcat(send_message,"/");
strcat(send_message,directory_name);
size = strlen(send_message) + 1;
csize = malloc(4 * sizeof(char));
csize = (char*)&size;
memcpy(&send_message,csize,4);

if((sent = send(sock, send_message, strlen(send_message) + 1, 0)) < 0)
     perror("write");



//*SERVER*//
while(1){
    count = recv(events[i].data.fd, buf, sizeof(buf),0);
    if(count == -1){
     //if errno = EAGAIN we have read all data. going back to main loop
          if(errno != EAGAIN){
                perror("read");
                done = 1;
          }
          break;
    }
    else if(count == 0){
     //End of file. The remote has closed the connections
      done = 1;
       break;
    }
    printf("received message %s and count %d\n", buf, count);
 }

if i comment these lines

csize = malloc(4 * sizeof(char));
csize = (char*)&size;
memcpy(send_message,csize,4);

I get this output:

 received message ▒�w�GETFILES /test and count 19

otherwise i get no output..Any ideas how to fix it and add the header so my server knows in advance how many bytes to read?

Upvotes: 0

Views: 2223

Answers (1)

Jonatan Goebel
Jonatan Goebel

Reputation: 1139

As commented already, the main problem is the use of strlen(), but there is some more error.

First the strlen() and other str functions could be used in this way.

strcpy(send_message + 4,"GETFILES ");
strcat(send_message + 4,"/");
strcat(send_message + 4,directory_name);
size = strlen(send_message + 4) + 1;

It is not a good way to fix it, but it is easier to understand why your code is not working.

This is unnecessary

csize = malloc(4 * sizeof(char));
csize = (char*)&size;
memcpy(&send_message,csize,4);

You could simply do this

memcpy(send_message,&size,4);

BUT, for good practice and portability, replace all your magic 4 for sizeof(int32_t).

send_message is an array, so you do not need to get it address (&send_message), it may work this way, but if it was a pointer instead of an array it would break your code.

And last, you are printing the entire buff, but you forgot that you have a 4 bytes header, and that is the main reason it do not print anything if you properly initialize the csize.

If you try to do this

printf("received message %s and count %d\n", buf+4, count);

It will probably works, but it do not mean it is correct.

Edit: I will not update my answer to not make it bigger and harder to you see your mistake, but consider @thuovila comments below and look for more informations about htonl ntohl and how to use them.

Upvotes: 1

Related Questions