PHcoDer PHcoDer - 2 months ago 6
C++ Question

Transfer array of integers over a socket in C/C++

This answer shows methods for sending and receiving integers over sockets in c/c++. Based on that I wrote following two functions, sendKInt and receiveKInt to send and receive K integers. But its not working correctly. Maybe my pointer arithmetic is wrong, but I cannot find the error. Please help with that, I am new to c++. Also see sendTill and receiveTill functions.

bool sendKInt(int fd, int num[], int k){ // returns true on success
int32_t conv[k];
char *data;
for (int i=0; i<k; i++){
conv[i] = htonl(num[i]);
char *data = (char*)conv;
}
return sendTill(sizeof(int32_t)*k, fd, data);
}

bool receiveKInt(int fd, int *num, int k){ // returns true on success
int32_t ret[k];
int toReturn = receiveTill(sizeof(int32_t)*k,fd,(char*)ret);
for (int i=0; i<k; i++){
num[i] = ntohl(ret[i]);
}
return toReturn;
}

bool sendTill(int size, int fd, char* dataU){ // dataU - dataUser pointer is NULL then create your own buffer and send random thing // returns true on successs
char *data;
int left = size;
int rc;
if(dataU == NULL){
data = (char*)malloc(size);
}else{
data = dataU;
}
while (left) {
rc = write(fd, data + size - left, left);
if (rc < 0){
cout <<"-1 in sendTill \n";
return false;
}
left -= rc;
}
return true;
}

bool receiveTill(int size, int fd, char* data){ // just give a char pointer after allocating size // returns true on success
int ret;
int left = size;
while (left) {
ret = read(fd, data + size - left, left);
if (ret < 0){
cout <<"-1 in receiveTill \n";
return false;
}
left -= ret;
}
return true;
}

Answer

A nicely working code:

bool sendKInt(int fd, int num[], int k){ // returns true on success
    std::vector<int32_t> conv(k);
    for (int i=0; i<k; i++){
        conv[i] = htonl(num[i]);
    }
    return sendTill(sizeof(int32_t)*k, fd, (char *)conv.data());
}

bool receiveKInt(int fd, int *num, int k){ // returns true on success
    int32_t *ret = (int32_t*)calloc (sizeof(int32_t), k);
    bool toReturn = receiveTill(sizeof(int32_t)*k,fd,(char*)ret);
    for (int i=0; i<k; i++){
        num[i] = ntohl(ret[i]);
    }
    free(ret);
    return toReturn;
}

bool sendTill(int size, int fd, char* dataU){ // dataU - dataUser pointer is NULL then create your own buffer and send random thing // returns true on successs
    char *data;
    int left = size;
    int rc;
    if(dataU == NULL){
        data = (char*)malloc(size);
    }else{
        data = dataU;
    }
    while (left) {
        rc = write(fd, data + size - left, left);
        if (rc < 0){
            cout <<"-1 in sendTill \n";
            return false;
        }
        left -= rc;
    }
    return true;
}

bool receiveTill(int size, int fd, char* data){ // just give a char pointer after allocating size // returns true on success
    int ret;
    int left = size;
    while (left) {
        ret = read(fd, data + size - left, left);
        if (ret < 0){
            cout <<"-1 in receiveTill \n";
            return false;
        }
        left -= ret;
    }
    return true;
}
Comments