Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读取inputstream时出错:软件导致连接中止';在java服务器中_Java_Android_Sockets_Inputstream_Serversocket - Fatal编程技术网

读取inputstream时出错:软件导致连接中止';在java服务器中

读取inputstream时出错:软件导致连接中止';在java服务器中,java,android,sockets,inputstream,serversocket,Java,Android,Sockets,Inputstream,Serversocket,我基本上是在安卓设备上托管服务器。客户端设备通过TCP连接到服务器并发送请求。服务器执行客户端请求的操作,然后将数据写回套接字。服务器不会终止连接,请求将通过套接字持续读取并回复 注意:每个请求消息的前4个字节包含实际消息/请求的长度 parseXmlInputAndExecuteCmd函数根据输入XML字符串的内容执行各种异步操作。这最终会导致“allowResponse”变量的布尔值更改为true,并生成一个特定的响应,该响应存储在名为“response”的字符串类型变量中。一旦布尔值“al

我基本上是在安卓设备上托管服务器。客户端设备通过TCP连接到服务器并发送请求。服务器执行客户端请求的操作,然后将数据写回套接字。服务器不会终止连接,请求将通过套接字持续读取并回复

注意:每个请求消息的前4个字节包含实际消息/请求的长度

parseXmlInputAndExecuteCmd函数根据输入XML字符串的内容执行各种异步操作。这最终会导致“allowResponse”变量的布尔值更改为true,并生成一个特定的响应,该响应存储在名为“response”的字符串类型变量中。一旦布尔值“allowResponse”变为true,线程将恢复执行并将响应写回套接字的OutputStream

其中一些异步操作包括连接和断开与公司VPN的连接。这可能是错误的原因吗

使用的一些类级别变量包括:

private volatile boolean allowResponse = false;
private String response;
服务器代码:

    private void startServer() {
    try {
        ServerSocket serverSocket = new ServerSocket(8080);
        while (true) {
            Socket connectionSocket = serverSocket.accept();
            BufferedInputStream bufInpStream = new BufferedInputStream(connectionSocket.getInputStream());
            BufferedOutputStream bufOutStream = new BufferedOutputStream(connectionSocket.getOutputStream());
            ByteArrayOutputStream contentLengthBaos = new ByteArrayOutputStream();
            int c;
            int count = 0;
            while ((c = bufInpStream.read()) != -1) {
                contentLengthBaos.write(c);
                count++;
                if (count == 4) {
                    int contLen = getMessageLength(contentLengthBaos);
                    String content = getMessageContent(bufInpStream, contLen);
                    parseXmlInputAndExecuteCmd(content);
                    count = 0;
                    while (!allowResponse) {
                        Thread.sleep(1000);
                    }
                    allowResponse = false;
                    byte[] responseDataBytes = response.getBytes();
                    int outputContLen = responseDataBytes.length;
                    byte[] contLengthBytes = ByteBuffer.allocate(4).putInt(outputContLen).array();
                    ByteArrayOutputStream o = new ByteArrayOutputStream();
                    o.write(contLengthBytes);
                    o.write(responseDataBytes);
                    byte finalOutPutBytes[] = o.toByteArray();
                    bufOutStream.write(finalOutPutBytes);
                    bufOutStream.flush();
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

@NonNull
private String getMessageContent(BufferedInputStream inFromClient, int contLen) throws IOException {
    ByteArrayOutputStream contentBaos = new ByteArrayOutputStream();
    byte[] contentBytes = new byte[contLen];
    for (int i = 0; i < contLen; i++) {
        contentBaos.write(inFromClient.read());
        ByteArrayInputStream bais = new ByteArrayInputStream(contentBaos.toByteArray());
        bais.read(contentBytes);
    }
    String content = new String(contentBytes);
    Log.d(TAG, "Content : " + content);
    return content;
}

private int getMessageLength(ByteArrayOutputStream contentLengthBaos) {
    byte[] firstFourBytesArr = contentLengthBaos.toByteArray();
    int contLen = new BigInteger(firstFourBytesArr).intValue();
    contentLengthBaos = new ByteArrayOutputStream();
    Log.d(TAG, "Content length: " + contLen);
    return contLen;
}
我得到以下错误堆栈跟踪:

W/System.err: java.net.SocketException: Software caused connection abort
                  at java.net.SocketInputStream.socketRead0(Native Method)
                  at java.net.SocketInputStream.socketRead(SocketInputStream.java:114)
W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:170)
W/System.err:     at java.net.SocketInputStream.read(SocketInputStream.java:139)
W/System.err:     at java.io.BufferedInputStream.fill(BufferedInputStream.java:248)
W/System.err:     at java.io.BufferedInputStream.read(BufferedInputStream.java:267)
                  at com.example.umathur.myapplication.MainActivity.startServer(MainActivity.java:192)
                  at com.example.umathur.myapplication.MainActivity.access$000(MainActivity.java:61)
                  at com.example.umathur.myapplication.MainActivity$1.run(MainActivity.java:139)
                  at java.lang.Thread.run(Thread.java:764)
我收到一个名为:
java.net.SocketException:软件导致的连接中止

我不确定在使用inputstream/outputstream时哪里出了问题。我在下面一行(stacktrace中提到的192行)得到错误:

在一些关于StackOverflow的类似问题中,我看到有人说这可能是公司防火墙配置问题?对吗?如果是这样,我该如何修复它


这可能是由于客户端代码(我没有访问权限)编写不正确造成的问题吗?

我尝试在我的pc上运行您的代码(不是在Android上),只需从浏览器调用“localhost:8080”即可

我想你也可以在你的设备里做同样的事情,只是为了知道这是否是客户端的问题

我编辑了这两行以避免长时间等待:

byte[] contentBytes = new byte[10];
    for (int i = 0; i < 10; i++) {
byte[]contentBytes=新字节[10];
对于(int i=0;i<10;i++){

我无法在本地重现您的问题,有些信息告诉我您的触发条件非常具体,但这里有一些东西可以帮助您诊断和修复您的问题

  • 您的代码:如果是您的代码,那么现有的web服务器代码应该可以工作。请尝试此代码中的代码
  • 防火墙:如果是防火墙,请联系您的网络管理员/团队等。如果您不知道它是谁,只需与公司中一位您认为合适的高调人士交谈,他们应该能够引导您找到合适的人
  • Socket SO_KEEPALIVE选项:我发现了一个,并在你的套接字上读到了这个问题。答案是关于PuTTY,但我认为在你的Java套接字上尝试可能是值得的。这当然是一个很容易尝试的结果。因此,试着通过调用
    connectionSocket.setKeepAlive(true)来添加;
    就在
    套接字连接Socket=serverSocket.accept()之后;

  • 您从何处启动服务器?
    应用程序
    活动
    ?我从Activity@UmangMathur您是否尝试过使用个人wifi进行测试?客户端可能在服务器读取数据时关闭了连接。您能告诉我您的java的确切版本吗?
    while ((c = inputStream.read()) != -1)
    
    byte[] contentBytes = new byte[10];
        for (int i = 0; i < 10; i++) {