Using sockets, I am not sure how to set the timeout?
thanks
int sock, connected, bytes_recieved;
char send_data [128] , recv_data[128];
SOCKADDR_IN server_addr,client_addr;
int sin_size;
int j = 0;
::socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(4000);
server_addr.sin_addr.s_addr = INADDR_ANY;
::bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
::listen(sock, 5);
::fflush(stdout);
while(1)
{
sin_size = sizeof(struct sockaddr_in);
connected = ::accept(sock, (struct sockaddr *)&client_addr, &sin_size);
while (1)
{
j++;
::send(connected, send_data, strlen(send_data), 0);
//dealing with lost communication ?
//and reastablishing communication
//set timeout and reset on timeout error
}
}
::closesocket(sock);
Best Answer. Given that on Policy server the default idle timeout for socket is 10min, I think 10 min is good vlaue for it.
This means that the server has been shut down, you used the wrong IP/DNS name, wrong port or the network connection to the server is down. A socket timeout is dedicated to monitor the continuous incoming data flow. If the data flow is interrupted for the specified timeout the connection is regarded as stalled/broken.
Using try/catch/finally. If you are a developer, so you can surround the socket connection part of your code in a try/catch/finally and handle the error in the catch. You might try connecting a second time, or try connecting to another possible socket, or simply exit the program cleanly.
Your Java socket is timing out means that it takes too long to get respond from other device and your request expires before getting response.
You need to use setsockopt
to set the SO_SNDTIMEO
and/or SO_RCVTIMEO
options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With