I’m trying to send an array of numbers over a TCP socket but they are coming out incorrect. I can’t figure out why it’s not receiving the correct numbers. Here’s what I have …
Client
// the number array to be sent
unsigned long numArray[10] = { htonl(1), htonl(2), htonl(3), htonl(4), htonl(5),
htonl(6), htonl(7), htonl(8), htonl(9), htonl(10) };
send(s, &numArray[0], sizeof(numArray), 0);
Server
// get the array of numbers
unsigned long numberArray[10];
int numberData = recv(new_fd, numberArray, sizeof(numberArray), 0);
if(numberData == -1) {
cout << "ERROR" << endl;
}
unsigned long* num = numberArray;
for(int i = 0; i < sizeOfIncomingData; i++) {
// this outputs a bunch of huge numbers, none of which are right...
cout << ntohl(*(num+i)) << endl;
}
It turns out my problem was coming from the previous data that was sent. The size being sent and the size received was different and therefore the entire array got messed up. Thank you for your advice everyone, can’t believe I didn’t catch that sooner…