简体   繁体   English

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

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

I'm developing a mobile application for android. 我正在为Android开发一个移动应用程序。 There, I creating socket and transfer data between android mobiles and windows application (which is run on pc,laptop). 在那里,我创建套接字并在android手机和Windows应用程序(在pc,笔记本电脑上运行)之间传输数据。 I'm using android 2.3 我正在使用android 2.3

Testing mobile samsung galaxy pop 测试移动三星银河流行
The pc and mobiles are connected via USB tithering. 电脑和手机通过USB连接线连接。
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. 但是一个问题是在应用程序中间,通过从系统中拔出移动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. 请大家给出您的想法。

The default socket read timeout is infinity, as it says in the Javadoc , "A timeout of zero is interpreted as an infinite timeout.". 缺省的套接字读取超时是无穷大,如Javadoc中所说,“零超时将被解释为无限超时”。 If you want a finite value, call Socket.setSoTimeout(). 如果需要一个有限值,请调用Socket.setSoTimeout().

I don't know the default timeout use this.socket.setSoTimeout(5000); 我不知道默认超时使用this.socket.setSoTimeout(5000); but you can try setting a higher timeout value. 但是您可以尝试设置更高的超时值。 Here 5000 miliseconds means 5 seconds. 5000毫秒表示5秒。 Hope this fixes your problem. 希望这可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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