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

Uma*_*hur 14 java sockets android inputstream serversocket

我基本上试图在Android设备上托管服务器.客户端设备通过TCP连接到服务器并发送请求.服务器执行客户端请求的操作,然后将数据写回套接字.连接未被服务器终止,并且将通过套接字连续读取请求并回复.

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

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

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

使用的一些类级变量是:

private volatile boolean allowResponse = false;
private String response;
Run Code Online (Sandbox Code Playgroud)

服务器代码:

    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;
}
Run Code Online (Sandbox Code Playgroud)

使用以下代码行启动服务器:

Thread serverThread = new Thread(new Runnable() {
        @Override
        public void run() {
            startServer();
        }
    });
    serverThread.start();
Run Code Online (Sandbox Code Playgroud)

我收到以下错误堆栈跟踪:

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)
Run Code Online (Sandbox Code Playgroud)

我收到一个错误: java.net.SocketException: Software caused connection abort

我不确定在输入流/输出流的使用中我出错了.我在以下行获取错误(堆栈跟踪中提到的行号192):

while ((c = inputStream.read()) != -1)
Run Code Online (Sandbox Code Playgroud)

在StackOverflow的一些类似问题中,我看到有人说这可能是企业防火墙配置问题?那是对的吗 ?如果是这样,我该如何解决?

这可能是客户端代码(我无权访问)无法正确写入的问题吗?

Fri*_*ghi 0

我尝试在我的电脑(而不是 Android)中运行您的代码,只需从浏览器调用“localhost:8080”即可正常工作。

我认为您可以在设备内部执行相同的操作,只是为了知道这是否是客户端问题。

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

byte[] contentBytes = new byte[10];
    for (int i = 0; i < 10; i++) {
Run Code Online (Sandbox Code Playgroud)