简体   繁体   中英

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

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.

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().

I don't know the default timeout use this.socket.setSoTimeout(5000); but you can try setting a higher timeout value. Here 5000 miliseconds means 5 seconds. Hope this fixes your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM