Java Socket returns EOF on low bandwidth

292 Views Asked by At

I'm making an Android app for playing online radio. I'm using Java Socket for connection and it's InputStream to read stream data. Actually there is HTTP request and response without length with content type such as audio/mpeg.

All works fine until bandwidth becomes low (using mobile internet or connected to slow/very remote server) - InputStream may return EOF (-1) at arbitrary time.

Please could you explain the nature of such behavior and possible ways to deal with it. I set read timeout (Socket.setSoTimeout), so why it doesn't throw timeout exception if the case is low bandwidth? Maybe there are another socket options that may help?

1

There are 1 best solutions below

1
On BEST ANSWER

That happens when you have Client connected and then It disconnects and you are trying to read from it. I handle this asking the length of the message, If it is negative, I restart the connection.

E.G.

//CLIENTS LOOP
while (true)
{
    ...

    //MESSAGE LOOP
    while ( true )
    {
         System.out.println(" -> WAITING FOR INCOMING MESSAGE.. ");
         len = inputStream.read(buffer_msg);

         if ( len < 0 )
             break;

         ...