0
votes

At server.cpp I have two int fields to send to client.cpp. I am sending in this way:

 if ((bytecount = send(*csock, portstring1, strlen(portstring1), 0)) == -1) {
    fprintf(stderr, "Error sending data %d\n", errno);
    goto FINISH;
    }

        sprintf(portstring2, "%d", ncount);
 if ((bytecount = send(*csock, portstring2, strlen(portstring2), 0)) == -1) {
    fprintf(stderr, "Error sending data %d\n", errno);
    goto FINISH;
    }

and at receiver side I use:

if((bytecount = recv(hsock, buffer, buffer_len, 0))== -1){
        fprintf(stderr, "Error receiving data %d\n", errno);
        goto FINISH;
    }
    printf("Positive counts are :");
    printf(" %s \n",buffer);

if((bytecount = recv(hsock, buffer2, buffer_len, 0))== -1){
        fprintf(stderr, "Error receiving data %d\n", errno);
        goto FINISH;
    }
    printf("Negative count is :");
    printf(" %s \n",buffer2);

But problem is that first rec() function catch both values sent from server and does not reach to second receive function. When I print data received from first rec function it show both values sent from server.

I tried using array to send both values togather but then converting int array to chat * became headache for me. Because send and rec function deal with char * values only. Not even string.

Any idea, how can I get both values at seperate level at client side?

1
If you only want to receive 4 bytes, then specify that in your first call, rather than buffer_len. But then, don't ignore bytecount. All you're guaranteed is that you got at least 1 byte. So you also need to add a loop to track how many bytes you have received and keep asking for more until you get the number you wanted. - Damien_The_Unbeliever
Do not use printf() with %s unless you set buffer to zero somewhere before each recv(). You are not sending a null character after the "integer" strings, since strlen() excludes it. - thuovila
@Damien_The_Unbeliever: byte size could not be restricted, it may vary each time! for my case - user123
@user174889 - then you need to develop a protocol so that you know how much data to receive in each attempt. Common protocols do this either by prefixing the data with length information or by having certain reserved byte sequences that indicate the end of each "message". By itself, in socket programming, you're dealing with a stream of bytes, and there's no guarantee (as you're already finding) of any correlation between the number of send calls at one end, and the number of recv calls needed at the other end. - Damien_The_Unbeliever

1 Answers

0
votes

There is no way for the receiver to know that you are sending 2 items or a single large item.You may need to implement your own protocol here.

If you know the size of each item you can try to read only that much from the recv buffer. If the item sizes vary you can simply send the size first and then the item, so that at the receiving end you can read the integer first and then set the buffer to that size and read the remaining.

In your case prefix each sent with the length of the string (4 bytes) at the receiving end read 4 bytes and convert it to int32 and you get the length of your message to be read. Now you can set the buffer size to the required size and read.

As Damien_The_Unbeliever mentioned you cannot make sure you have received the full message in one go, you may have to loop through the read.