Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Socket Timeout?

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);
like image 528
jdl Avatar asked Mar 23 '12 22:03

jdl


People also ask

What is a good socket timeout?

Best Answer. Given that on Policy server the default idle timeout for socket is 10min, I think 10 min is good vlaue for it.

What happens on socket timeout?

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.

How do I fix socket timeout error?

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.

What is socket timeout on phone?

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.


1 Answers

You need to use setsockopt to set the SO_SNDTIMEO and/or SO_RCVTIMEO options.

like image 63
Jon Avatar answered Nov 15 '22 02:11

Jon