0

I'm sending an integer array from client to server by socket. Like this ;

int arr[3] = {5, 4, 1};


send(sock , &arr, 3 , 0);   //sending the votes count

and receiving in server like so

recv(new_socket, arr1, 3, 0);

I'm getting correct value in arr1[0] but in arr1[1] I'm getting 66 and arr1[2] I'm getting 67

I don't know how this happening and where 66 and 67 is coming from

8
  • 2
    That 3 is a byte count, not an element count. the two are only synonymous if you're sending simple octets. Chances are you're not even sending one full int, much less three. Unrelated, &arr could just be arr in the send call. Won't really matter, but still. Commented Apr 25, 2018 at 18:08
  • 2
    you need send(sock , &arr,sizeof(arr) , 0); Commented Apr 25, 2018 at 18:08
  • 2
    note - in general you need to send a length indicator before the message. TCP is a stream protocol not a message protocol, you cannot expect to receive the same messages tht you send, only the same bytes in the same order Commented Apr 25, 2018 at 18:09
  • 1
    unless it is a datagramm mode (udp) Commented Apr 25, 2018 at 18:11
  • 1
    You need to use sizeof in send and recv. Commented Apr 25, 2018 at 18:31

1 Answer 1

2

send() and recv() operate on bytes only. You are sending and receiving 3 bytes, but your array is actually sizeof(int) * 3 bytes instead (where sizeof(int) is usually 4 on many platforms). You need to use sizeof() on both ends, eg:

send(sock, &arr, sizeof(arr), 0);

recv(new_socket, &arr1, sizeof(arr1), 0);

However, you also have to pay attention to return values, because send() can send fewer bytes than requested, and recv() can receive fewer bytes than requested. TCP is stream-oriented, not message-oriented. You should be calling send() and recv() each in a loop, re-calling each one until all expected bytes have been sent/received in full, eg:

ssize_t sendAll(int sckt, void *data, size_t length)
{
    char *bytes = (char*) data;
    while (length > 0)
    {
        ssize_t sent = send(sckt, bytes, length, 0);
        if (sent == -1) return -1;
        bytes += sent;
        length -= sent;
    }
    return 0;
}

int arr[3] = {5, 4, 1};
sendAll(sock, &arr, sizeof(arr));

ssize_t recvAll(int sckt, void *data, size_t length)
{
    char *bytes = (char*) data;
    while (length > 0)
    {
        ssize_t recvd = recv(sckt, bytes, length, 0);
        if (recvd <= 0) return recvd;
        bytes += recvd;
        length -= recvd;
    }
    return 1;
}

int arr1[3];
recvAll(new_socket, &arr1, sizeof(arr1));
Sign up to request clarification or add additional context in comments.

3 Comments

also a conversion may be needed if endianness of sender/receiver sides are different
Seems to be finde. However, I would use size_t as type for length and ssize_t as type for sent/recvd. It is also a little bit confusing that both functions use different return values. (sendAll: 0/OK, -1/IO Error; recvAll: 1/OK, 0/Unexpected EOF, -1/IO Error)
@JojOatXGME many of the standard socket functions return 0 on success and -1 on error, so I followed that same convention for sendAll(). Since recv() has different semantics, recvAll() is similarly different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.