java.io.IOException: 对等方重置连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7792457/
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
java.io.IOException: Connection reset by peer
提问by Sonhja
I'm trying to manage a connection between my phone and another bluetooth device. I'm doing all this using java Android. This is the code I used for connecting the socket using my device:
我正在尝试管理我的手机和另一个蓝牙设备之间的连接。我正在使用 java Android 完成所有这些工作。这是我用于使用我的设备连接套接字的代码:
First I find the Bluetooth device and I create the socket:
首先,我找到蓝牙设备并创建套接字:
BluetoothDevice btNonin = null;
for (BluetoothDevice device : pairedDevices)
{
if (device.getName().contains("Nonin"))
{
// We found the device
exito = true;
try
{
// We create the socket
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
}
catch (Exception e)
{
Dialogs.showInfoDialog(NONIN_OFF, this);
}
}
}
Afther that I create the data bytes I want the remote bluetooth to receive using some code to convert ASCII to byte:
在创建数据字节之后,我希望远程蓝牙接收使用一些代码将 ASCII 转换为字节:
String[] parts = mensaje.split(" ");
String res = "";
if(parts != null && parts.length > 0)
{
for (String s : parts)
{
if (s.length() != 2)
break;
byte izq, der;
izq = (byte)char2ascii(s.charAt(0));
der = (byte)char2ascii(s.charAt(1));
byte aux2 = (byte)((izq << 4) + der);
res += (char)aux2;
}
}
And then I send the data to the bluetooth device:
然后我将数据发送到蓝牙设备:
// We send the data bytes
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
dOut.writeBytes(res);
dOut.flush();
Until here it works fine. It sends the data bytes to my device. But then I want to wait for any response from my device, and then I try this:
直到这里它工作正常。它将数据字节发送到我的设备。但后来我想等待我的设备的任何响应,然后我试试这个:
//Waiting for response
DataInputStream dIn = new DataInputStream(socket.getInputStream());
try
{
byte response = '\u0000';
while (dIn.readByte() == '\u0000')
{
response = dIn.readByte();
}
Dialogs.showInfoDialog("Response: " + response, this);
}
catch (Exception e)
{
Dialogs.showInfoDialog("No se ha recibido respuesta: " + e.toString(), this);
}
But then, on the line where I make dIn.readByte it shows the message Error:
但是,在我制作 dIn.readByte 的那一行上,它显示了错误消息:
java.io.IOException: Connection reset by peer
And I don't know why the connection is reset or what happens, as I can debug the line:
而且我不知道为什么连接被重置或发生了什么,因为我可以调试该行:
DataInputStream dIn = new DataInputStream(socket.getInputStream());
With no mistakes, so I guess the socket is still opened... What is happening here?
没有错误,所以我猜套接字仍然打开......这里发生了什么?
Thank you so much for your help!
非常感谢你的帮助!
回答by user207421
There are several causes of this problem. The typical cause is that you have written to a connection which has already been closed by the peer. In other words, an application protocol error.
这个问题有几个原因。典型的原因是您已写入已被对等方关闭的连接。换句话说,应用程序协议错误。
Also your exception handling needs work. If you get any IOException on a socket other than a timeout you must close it, it is dead.
您的异常处理也需要工作。如果在超时以外的套接字上遇到任何 IOException,则必须关闭它,它已死。
回答by Sonhja
回答by Anantha Sharma
remove the line dOut.flush();
this is causing the connection reset.
删除dOut.flush();
导致连接重置的行。