Search code examples
javaandroid

SocketTimeoutException


In my application I use a socket to communication with another device. All the time in I have a SocketTimeoutException . This communication I did in service

This is a logs :

this line couse a SocketTimeOutException :

  while ((bytesRead = inputStream.read(content)) != -1) {

Solution

  • Your socket is timing out means that it takes too long to get respond from other device and your request expires before getting response.To tackle this you need to give manual socket timeout like search for how to provide socket timeout of the library you are using. If using socket.io then you need to add

    socket.timeout(12000);
    

    Here 12000 are milli seconds or if you are using OkHttp Client then you can add:

    clientBuilder.connectTimeout(60, TimeUnit.SECONDS);
    

    Or whichever socket library you are using there is method to give timeout manually so just type your socket name with which you created socket and place '.' after that you have suggestions for available methods in that search for timeout and use that method.

    Here is the link use that library demo is also there:

    https://github.com/socketio/socket.io-client-java