I have a client program running on Unix which creates a socket and connects to a server program running on a Windows machine, then starts performing operations which produce constantly changing double values. I use a timer, and every 5 milliseconds I call a function which takes whatever my value is at that time, converts it to a string, and sends it to the second computer. The server program receives the value from the client and prints to the screen.
The client and server successfully connect, and initially the sending and receiving works. But after a short time, the server's print messages to the screen slow down, and then the messages showing the new values stop altogether.
On the client side program, here's the function I call to connect to the server:
Code:
if ((ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
{
perror("Client-socket() error");
}
else
printf("Client: socket() is OK.\n");
clientService.sin_family = AF_INET;
clientService.sin_port = htons(55555);
clientService.sin_addr.s_addr = inet_addr("198.9.200.2");
if (connect(ConnectSocket, (struct sockaddr *)&clientService, sizeof(clientService)) < 0)
{
printf("Client: Failed to connect.\n");
close(ConnectSocket);
exit(-1);
}
else
printf("Client: connect() is OK.\n");
printf("Client: Connected to server...\n");
//send(ConnectSocket, sendbuf, strlen(sendbuf), 0);
}
and for this the function called every 5 milliseconds by the client to send data:
Code:
int bytesRecv;
int bytesSent;
char recvbuf[200] = "";
char sendbuf1[200];
// convert data to string then send
sprintf(sendbuf1,"%20.4f",val);
//sending
send(ConnectSocket, sendbuf1, strlen(sendbuf1), 0);
bytesRecv = recv(ConnectSocket, recvbuf, 32, 0);
if(bytesRecv < 0)
{
perror("Client-read() error");
close(ConnectSocket);
exit(-1);
}
else if (bytesRecv == 0)
{
printf("Server program has issued a close()\n");
close(ConnectSocket);
exit(-1);
}
And for the server, this is the part of the code I use for receiving the data:
Code:
for(;;) {
/* wait for client’s message */
bytesRecv = recv(socket, recvbuf, 200, 0);
printf("value received: %s\n",recvbuf);
if (bytesRecv == SOCKET_ERROR)
{
printf("Server: recv() error %ld.\n", WSAGetLastError());
exit(-1);
}
Does anyone have an idea what could be wrong, or what I could do to check for what is wrong?