java.net.SocketException:sendto失败:EPIPE(管道损坏)发送字符以检查套接字连接时 [英] java.net.SocketException: sendto failed: EPIPE (Broken pipe) When sending character to check socket connection

查看:456
本文介绍了java.net.SocketException:sendto失败:EPIPE(管道损坏)发送字符以检查套接字连接时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的系统包括:Android Mobile上的套接字服务器和PC上的套接字客户端.为了检查与客户端的连接,我每隔1秒从服务器发送一个" "字符.

I develop a system include: socket server on Android Mobile and socket client on PC. And to check the connection with client, I had sent a " " character every 1 second from server.

但是有时候,我会遇到一个例外:

But sometimes, I got the exception:

java.net.SocketException: sendto failed: EPIPE (Broken pipe)    
    at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506)    
    at libcore.io.IoBridge.sendto(IoBridge.java:475)    
    at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)    
    at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)    
    at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)    
    at java.io.OutputStream.write(OutputStream.java:82)    
    at com.foxconn.cnsbgit.mobileterminal.MainActivity$ServerThread.run(MainActivity.jaa:527)    
    at java.lang.Thread.run(Thread.java:856)    
Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)    
    at libcore.io.Posix.sendtoBytes(Native Method)    
    at libcore.io.Posix.sendto(Posix.java:151)    
    at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)    
    at libcore.io.IoBridge.sendto(IoBridge.java:473)    
    ... 6 more

正在发送Thread检查连接的信息如下:

The sending Thread to check connection is following:

while (true) {
    if (client != null) {
         try {
              client.getOutputStream().write(" ".getBytes()); // Get exception when sending character
              Thread.sleep(1000);
              mHandler.post(new Runnable() {
                  @Override
                  public void run() {
                      txtConnectionStatus.setText(R.string.smoConnectOK);
                  }
              });
         } catch (Exception e) {
              e.printStackTrace(); // Get exception at here
              mHandler.post(new Runnable() {
                  @Override
                  public void run() {
                      if !txtConnectionStatus.getText().toString().contains("FAIL")) {
                           txtConnectionStatus.setText(R.string.connectionFailString);
                      }
                  }
              });
              try {
                   Thread.sleep(5000);
              } catch (InterruptedException e1) {
                   e1.printStackTrace();
              }
          }
     }
}

更新: 然后,我输入了数据并将数据发送给客户端.当心跳和数据同时发送时,连接会丢失吗? :

Updated: Then I had input and sent data to client. Is connection lost when both heartbeat and data send at the same time? :

public class SendDataThread implements Runnable {

    @Override
    public void run() {
        try {
            if (client != null) {
                sendDataStream = client.getOutputStream();
                sendDataStream.write(dataSend); //dataSend is a byte array
                sendDataStream.flush();

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        edtCommand.selectAll();
                    }
                });
            }
        } catch (final Exception e) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    txtRec.setText(e.toString());
                }
            });
        }
    }
}

推荐答案

当Android处于空闲状态时,设备将锁定,然后进入深度睡眠模式.在深度睡眠模式下,Android系统关闭现有的网络连接,例如TCP或UDP.如果您的应用程序连接到服务器,它将失去与服务器的连接,并尝试根据为客户端配置的重新连接尝试方法来重新连接.但是,如果您的应用是服务器,则所有客户端都将失去与服务器的连接,您必须在服务器中再次启动套接字,然后尝试从客户端再次连接.

When an Android is kept idle, the device locks and then it goes to deep sleep mode. In deep sleep mode the Android system, closes the existing network connections like TCP or UDP. If your app is connected to a server, it loses connection to the server and tries to reconnect based on the reconnect attempt methods configured for the client. But if your app is the server, all the client will lose connection to the server, and you have to start the socket again in server and try to connect once again from the clients.

长时间保持打开TCP连接对于移动设备不是一个好的选择,因为TCP连接与睡眠的计算机无法很好地交互.问题场景是这样的:您的Android用户在应用程序运行时将其Android设备置于睡眠状态,然后远程用户的程序(或TCP连接另一端的任何程序)通过TCP流发送一些数据.远程用户的程序永远不会从Android设备获得任何ACK,因为Android设备当然处于睡眠状态,因此远程设备的TCP堆栈假定它发送的TCP数据包一定已经丢失,并且它会通过增加超时时间来做出响应,减小其TCP窗口大小(即一次允许飞行中的TCP数据包数),然后重新发送TCP数据包.但是Android设备仍处于睡眠状态,因此同样的事情再次发生.结果是,几分钟后,TCP连接的远程端速度变慢,以至于即使要唤醒Android设备,TCP连接也可能太慢而无法使用-在这一点上,您的程序将需要关闭陷入困境的TCP连接并重新启动一个新连接,那么为什么还要尝试使其保持打开状态呢?

Keeping a TCP connection open for extended periods may not be a good option for a mobile device, because TCP connections don't interact well with computers that go to sleep. The problem scenario would be this: your Android user puts his Android device to sleep while your app is running, and then the remote user's program (or whatever is at the other end of the TCP connection) sends some data over the TCP stream. The remote user's program never gets any ACKs back from the Android device, because of course the Android device is asleep, so the remote device's TCP stack assumes that the TCP packets it sent must have been lost, and it responds by increasing its timeout period, decreasing its TCP window size (aka number-of-TCP-packets-allowed-in-flight-at-once), and resending the TCP packets. But the Android device is still asleep, and thus the same thing happens again. The upshot is that a few minutes later, the remote end of the TCP connection has slowed down to the point where even if the Android device was to wake up, the TCP connection will likely be too slow to be usable -- at which point your program will need to close the bogged-down TCP connection and start up a fresh one anyway, so why bother trying to keep it open?

解决方案

获取PARTIAL_WAKE_LOCK,并在屏幕关闭时捕获.然后 禁用并重新启用wifi.这有效,因为仅过滤器 萤幕关闭时会亮起,因此请以萤幕开始wifi 关闭将一直保持工作状态,直到屏幕再次关闭.

Acquire a PARTIAL_WAKE_LOCK, and trap when the screen goes off. Then disable and reenable the wifi. This works because the filter only turns on when the screen goes off, so starting wifi with the screen off will keep it working until the screen goes off again.

这篇关于java.net.SocketException:sendto失败:EPIPE(管道损坏)发送字符以检查套接字连接时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆