java android中java.net.Socket的默认超时值是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12128361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:41:56  来源:igfitidea点击:

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

javaandroidsocketsandroid-networkingsocket-timeout-exception

提问by SIVAKUMAR.J

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

我正在为 android 开发一个移动应用程序。在那里,我创建了 android 手机和 windows 应用程序(在 pc、笔记本电脑上运行)之间的套接字和传输数据。我正在使用安卓 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.

测试手机三星galaxy pop
电脑和手机通过USB 网络连接。
数据传输正确。
但一个问题是在应用程序中间,通过从系统中拔下移动 USB 接口取消了锁定,然后我抛出异常。

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

但有时它不会通过任何异常它只是等待,套接字也没有关闭等待读取数据
所以请在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.


然后它返回0。


Please all are give your ideas.


请大家给出你的想法。

采纳答案by user207421

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

默认套接字读取超时是无穷大,正如它在Javadoc 中所说的那样,“零超时被解释为无限超时。”。如果你想要一个有限的值,调用Socket.setSoTimeout().

回答by user1613360

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.

我不知道默认超时的使用,this.socket.setSoTimeout(5000);但您可以尝试设置更高的超时值。这里 5000 毫秒意味着 5 秒。希望这能解决您的问题。