简体   繁体   English

java.io.IOException:由peer重置连接

[英]java.io.IOException: Connection reset by peer

I'm trying to manage a connection between my phone and another bluetooth device. 我正在尝试管理手机和另一个蓝牙设备之间的连接。 I'm doing all this using java Android. 我正在使用java Android做这一切。 This is the code I used for connecting the socket using my device: 这是我用于使用我的设备连接套接字的代码:

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的行上,它显示消息Error:

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! 非常感谢你的帮助!

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,你必须关闭它,它已经死了。

Ok, I tried adding some wait() functions and it seems it works... As it was a problem between timeouts of reading and writing. 好吧,我尝试添加一些wait()函数,它似乎有效......因为它是读写超时之间的问题。 I think this post should work... 我认为这篇文章应该有效......

remove the line dOut.flush(); 删除行dOut.flush(); this is causing the connection reset. 这导致连接重置。

暂无
暂无

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

相关问题 Cassandra-unit:java.io.IOException:由peer重置连接 - Cassandra-unit : java.io.IOException: Connection reset by peer 什么时候抛出“java.io.IOException:Connection reset by peer”? - When is "java.io.IOException:Connection reset by peer" thrown? ElasticSearch RestHighLevelClient 抛出 java.io.IOException: Connection reset by peer - ElasticSearch RestHighLevelClient throws java.io.IOException: Connection reset by peer 什么时候出现java.io.IOException:用Netty抛出的对等重置连接? - When is java.io.IOException: Connection reset by peer thrown with Netty? 异步http客户端中的对等方重置java.io.IOException连接 - java.io.IOException connection reset by peer in asynchronous http client BrowserMob代理警告和异常java.io.IOException:对等重置连接 - BrowserMob Proxy warning and exception java.io.IOException: Connection reset by peer SSTable加载器流式传输失败,给出java.io.IOException:对等体重置连接 - SSTable loader streaming failed giving java.io.IOException: Connection reset by peer java.io.IOException:在Weblogic服务器上使用AsyncRestTemplate时,连接被对等方重置 - java.io.IOException: Connection reset by peer' when using AsyncRestTemplate on weblogic server Flume:来自下游的意外异常。 java.io.IOException:对等重置连接 - Flume : Unexpected exception from downstream. java.io.IOException: Connection reset by peer 间歇性 java.io.IOException:从 Amazon EC2 内部的 jdk.internal.net.http.HttpClientImpl.send 中的连接重置(不是对等连接重置) - Intermittent java.io.IOException: Connection reset (not connection reset by peer) in jdk.internal.net.http.HttpClientImpl.send from inside Amazon EC2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM