OS: Linux
Language: C
My client repeatedly sends arrays of integers to the server. The size of the array varies with each send from the client. The array of signed integers (defined as INT32) is packaged in the data_ptr variable and the array length is stored in the data_ptr_size variable. The first seven elements of the array are known to me so I use them to validate what the server is receiving.
The issue I am having is that when I print out the first 7 array elements (integers) received by the server, they do not always match what the client has sent. However, I do believe the data is correct and in the order it was sent. For example, I know that data_ptr[0] always equals 999999999 and data_ptr[6] always equals -999999999.
This issue has something to do with timing or maybe I need to do a handshake between messages. I appreciate your input.
Client Side function to send array of integers to the server.
Code:
int send_packet(INT32 *data_ptr, INT32 data_ptr_size)
{
int hSocket;
// Create socket
hSocket = SocketCreate();
if(hSocket == -1)
{
//printf("Could not create socket\n");
return 1;
}
printf("Socket is created\n");
// Connect to remote server
if (SocketConnect(hSocket) < 0)
{
perror("connect failed.\n");
close(hSocket);
return 1;
}
printf("Sucessfully conected with server\n");
// here send the size of the array
if(send(hSocket,&data_ptr_size,sizeof(INT32),0) < 0)
{
printf("Send data_ptr_size Failed\n");
close(hSocket);
return 1;
}
printf("Send data_ptr_size Success\n");
// here send the entire array
if(send(hSocket, data_ptr, data_ptr_size*sizeof(INT32), 0) < 0) {
printf("Send Failed\n");
close(hSocket);
return 1;
}
// print some info for verification purpose
printf("size: %d %d\n",data_ptr_size,data_ptr_size*sizeof(INT32));
printf("data_ptr[0]: %d\n",data_ptr[0]); // <--known value
printf("data_ptr[6]: %d\n",data_ptr[6]); // <--known value
printf("-------------------------------\n");
close(hSocket);
return 0;
}
The server code is shown below:
Code:
int main(int argc, char *argv[])
{
INT32 *data_ptr = NULL;
int socket_desc, sock, clientLen, read_size, array_size;
int total = 0;
struct sockaddr_in client;
//Create socket
socket_desc = SocketCreate();
if (socket_desc == -1)
{
printf("Could not create socket");
return 1;
}
printf("Socket created\n");
// Bind
if( BindCreatedSocket(socket_desc) < 0)
{
//print the error message
perror("bind failed.");
return 1;
}
printf("bind done\n");
// Listen
listen(socket_desc, 3);
printf("Waiting for incoming connections...\n");
// Accept and incoming connection.
while(1)
{
clientLen = sizeof(struct sockaddr_in);
// Accept connection from an incoming client
sock = accept(socket_desc,(struct sockaddr *)&client,(socklen_t*)&clientLen);
if (sock < 0)
{
perror("accept failed");
return 1;
}
printf("Connection accepted\n");
// read the size of array
read_size = read(sock,&array_size,sizeof(INT32));
if(read_size==0)
{
printf("error reading rec_val\n");
exit(1);
}
// create memory to store the array
data_ptr = (INT32*)(malloc(array_size*sizeof(INT32)));
if(data_ptr==NULL)
{
printf("ERROR: memory failed to allocate.\n");
exit(0);
}
// Read the array using the array size and data_ptr
read_size = 0;
total = 0;
while ((read_size = recv(sock, (void *)data_ptr, array_size*sizeof(INT32), 0)) > 0)
{
total += read_size;
}
// print out array information
printf("size: %d %d\n",array_size,total);
// the first 7 elements of the array are known and used of verification
printf("data_ptr[0] : %d\n",data_ptr[0]);
printf("data_ptr[1] : %d\n",data_ptr[1]);
printf("data_ptr[2] : %d\n",data_ptr[2]);
printf("data_ptr[3] : %d\n",data_ptr[3]);
printf("data_ptr[4] : %d\n",data_ptr[4]);
printf("data_ptr[5] : %d\n",data_ptr[5]);
printf("data_ptr[6] : %d\n",data_ptr[6]);
printf("-------------------------------\n");
free(data_ptr);
data_ptr=NULL;
close(sock);
}
return 0;
}