what is the default time out value of java.net.Socket in android?

SIVAKUMAR.J picture SIVAKUMAR.J · Aug 26, 2012 · Viewed 18.1k times · Source

I'm developing a mobile application for android. There, I creating socket and transfer data between android mobiles and windows application (which is run on pc,laptop). I'm using android 2.3

Testing mobile samsung galaxy pop
The pc and mobiles are connected via USB tithering.
The data transfer correctly.
But one problem is in the middle of application the tithering cancelled by unplug the mobile usb caple from system,then i throws exception.

But some times it does not through any exception it simply waits,Socket also not closed waits for data for reading
So please send the default socket time out time in android


My sample codes are given below

    Socket socket=null;
    DataOutputStream dos=null;
    OutputStream os=null;
    InputStream is=null;
    DataInputStream dis=null;

    try
    {
        Log.i(tagName, "b4 creating socket");
        socket=new Socket(this.getIpAddress(),this.getPort_number());                   
        is=socket.getInputStream();             
        dos=new DataOutputStream(os);       
        dis=new DataInputStream(is);            
    }
    catch(UnknownHostException unknown_ex)
    {
        Log.i(tagName, "Exception host unknown : "+unknown_ex.toString());
        unknown_ex.printStackTrace();           
    }
    catch(IOException ioe_ex)
    {
        Log.i(tagName, "ioe Exception : "+ioe_ex.toString());           
        ioe_ex.printStackTrace();           
    }
    catch(Exception ex)
    {
        Log.i(tagName, "Exception : "+ex.toString());       
        ex.printStackTrace();       
    }

    if(dos!=null)
    {
        try
        {
            dos.close();
        }
        catch(Exception ex)
        {

        }
    }

    if(dis!=null)
    {
        try
        {
            dis.close();
        }
        catch(Exception ex){}
    }
    if(socket!=null)
    {
        try
        {
            socket.close();
        }
        catch(Exception ex)
        {

        }
    }

    socket=null;dos=null;dis=null;      
    Log.i(tagName, "MySocketConnection.connectMe() - end");


When I use the code

Socket.getSoTimeout()


Then it returns 0.


Please all are give your ideas.

Answer

user207421 picture user207421 · Aug 26, 2012

The default socket read timeout is infinity, as it says in the Javadoc, "A timeout of zero is interpreted as an infinite timeout.". If you want a finite value, call Socket.setSoTimeout().