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,笔记本电脑上运行)之间创建套接字和传输数据。我正在使用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.

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

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

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

请大家提出你的想法。

2 个解决方案

#1


5

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

更多相关文章

  1. 我无法让这个简单的ajax代码工作
  2. javascript实现拖动层效果代码(许愿墙)
  3. 牛客网Java刷题知识点之同步方法和同步代码块的区别(用synchroniz
  4. 在java自动生成hashCode代码问题? 请大神赐教
  5. 如何修改代码,使其仅打印单元格E和F?
  6. Java常量表达式相关的编译优化代码
  7. java代码获知该方法被哪个类、哪个方法、在哪一行调用
  8. 我的构建可以规定我的代码覆盖范围永远不会变得更糟吗?
  9. 基于Java的应用程序的GUI测试工具

随机推荐

  1. init.rc
  2. Android已经完全不受Google控制了
  3. Android之gif动画实现
  4. Android 调用手机系统照相机拍照
  5. Android(安卓)实现环形进度按钮circular-
  6. Android Zygote Fork
  7. 【引用】Android(安卓)CTS 测试常见问题
  8. GitHub 优秀的 Android(安卓)开源项目
  9. Android Studio 2.0和Android Studio 2.1
  10. Android TextView背景色、圆角、内部填充