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

SIV*_*R.J 8 java sockets android android-networking socket-timeout-exception

我正在为Android开发一个移动应用程序.在那里,我在Android手机和Windows应用程序(在PC,笔记本电脑上运行)之间创建套接字和传输数据.我正在使用android 2.3

测试移动三星galaxy pop
电脑和手机通过USB连接进行连接.
数据传输正确.
但是一个问题是在应用程序中间通过拔掉系统中的移动usb caple而取消了标题,然后我抛出了异常.

但有时它不会通过任何异常它只是等待,Socket也没有关闭等待数据读取
所以请在android中发送默认的套接字超时时间


我的示例代码如下

    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");
Run Code Online (Sandbox Code Playgroud)


当我使用代码

Socket.getSoTimeout()
Run Code Online (Sandbox Code Playgroud)


然后它返回0.


请大家提出你的想法.

use*_*421 5

默认的套接字读取超时是无穷大,正如它在Javadoc中所说的那样,"超时为零被解释为无限超时.".如果您想要有限值,请致电Socket.setSoTimeout().

  • 你能指出这是用Javadoc写的吗? (2认同)