我正在为安卓开发一个移动应用程序。在那里,我创建了套接字并在 android 手机和 Windows 应用程序(在 pc、笔记本电脑上运行)之间传输数据。我正在使用安卓 2.3
测试手机samsung galaxy pop
电脑和手机通过 USB Tithering 连接。
数据传输正确。
但是一个问题是在应用程序中间,通过从系统中拔下移动 USB Caple 取消了十分之一,然后我抛出异常。
但有时它不会通过任何异常它只是等待,Socket也没有关闭等待数据读取
所以请在android中发送默认的socket超时时间
我的示例代码如下
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");
当我使用代码时
Socket.getSoTimeout()
然后它返回 0。
请大家给出你的想法。