I m building a client and a server program that exchanges data over TCP and I m having trouble sending an ACK-confirmation from the server back to the client when an operation is successfull. I ve managed to send a struct with various members from the client to the server, then the server should respond by sending an integer confirming the ID of the struct back to the client.
In server.c I have this function:
int sendACK(int socket, int ack_int){
int com_result;
int ACK = htonl(ack_int);
printf("
Sending ACK with value: %i", ack_int);
com_result = send(socket, &ACK, sizeof(ACK), 0);
if (com_result == -1) {
perror("
[ERROR] Send ACK");
return 0;
}
return 1;
}
This function is called by another function in server.c like this:
int ACK = myStruct->ID;
sendACK(socket, ACK);
So, if I have just recieved a struct with ID: 2, the output will be:
Sending ACK with value: 2
In client.c I recieve the ACK like this:
int ACK = 0;
data_size = sizeof(myStruct.ID);
com_result = read(client_socket, &ACK, data_size);
ACK = ntohl(ACK);
if (com_result == -1) {
perror("
[ERROR] Read");
} else {
printf("
Recieved %i of %i bytes", com_result, data_size);
if (ACK != myStruct.ID) {
printf("
[ERROR] Invalid ACK (%i).", ACK);
}
}
But the ACK is always recieved with a value of 1, so the output from client is:
Recieved 4 of 4 bytes
ERROR] Invalid ACK (1).
Why isn t the integer recieved with a value of 2, and how may I fix this?