简体   繁体   English

IOException:管道损坏

[英]IOException: Broken pipe

I am implmenting server side application which communicates with Android app. 我正在实现与Android应用程序通信的服务器端应用程序。 The Android app has been implemented before that originally communicate with the C++ Server. 在最初与C ++ Server通信之前,已经实现了Android应用。 Now I want to replace the C++ server with java Code. 现在,我想用java代码替换C ++服务器。 The Android app communicates with the server to get the person authenticated by his card in the card reader. Android应用程序与服务器通信,以在读卡器中通过其卡进行身份验证。

The authentification protocol contains of serveral steps of communication between the app and the server to be completed successfully. 身份验证协议包含应用程序和要成功完成的服务器之间的服务器通信步骤。

message between the app and the server has the following form: 应用程序和服务器之间的消息具有以下形式:

<type> 0x00 0x00 0x00 <length> 0x00 0x00 0x00 [<data>]
  1. First the app sends a request of type 1 to build connection to the sim card in the card reader. 首先,该应用发送类型为1的请求,以建立与读卡器中sim卡的连接。
  2. Then the clientSocket on the server sends a reponse of type 0 that header has received the last message. 然后,服务器上的clientSocket发送类型为0的响应,表明标头已收到最后一条消息。
  3. Afterwards, the server receives a new request of type 2 to send the ATR (Answer To Rest) of the sim card to the app. 此后,服务器收到类型2的新请求,以将sim卡的ATR(Answer To Rest)发送到应用程序。
  4. the clientSocket of the server sends message of type 2 to the app. 服务器的clientSocket将类型2的消息发送到应用程序。 . . . . . . . . . . . . .

At the end I want to close the clientSocket and the serverSocket on the server side. 最后,我想在服务器端关闭clientSocket和serverSocket。

I have added the important code: 我添加了重要的代码:

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.Arrays;


public class Test {

    private final static int RECEIVE_BUFFER_LENGTH = 512;
    public final static int MESSAGE_MAXIMUM_LENGTH = 256;
    private final static int MESSAGE_HEADER_LENGTH = 8;


    public static void main(String args[]) throws Exception {
        ServerSocket serverSocket = new ServerSocket(3003);
        while (true) {
            Socket clientSocket = serverSocket.accept();
            ByteBuffer receiveBuffer = ByteBuffer.allocate(Test.RECEIVE_BUFFER_LENGTH);
            int readBytes = 0;
            InputStream bufferedInputStream = new BufferedInputStream(clientSocket.getInputStream());
            while (true) {
                ByteBuffer answerBuffer = null;
                readBytes = bufferedInputStream.read(receiveBuffer.array(), receiveBuffer.position(),
                        receiveBuffer.remaining());
                System.out.println("readBytes: " + readBytes); // Here I am getting 9 then -1.
                if (readBytes < 0) {
                    break;
                }
                // Here I am processing the message.
                // .......
                // after ending the processing send a reponse to the Android app.

                try {
                    answerBuffer = ByteBuffer.allocate(Test.MESSAGE_HEADER_LENGTH);
                    answerBuffer.put((byte) 0x00); // at position 0
                    DataOutputStream dOut = new DataOutputStream(clientSocket.getOutputStream());
                    dOut.writeBytes(Arrays.toString(answerBuffer.array()));
                    dOut.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("The sent answer to the client:  " + Arrays.toString(answerBuffer.array()));
            }

        }
    }

}

The outputs: 输出:

readBytes: 9
The sent answer to the client:  [0, 0, 0, 0, 0, 0, 0, 0]
readBytes: -1

Error I am getting the following error in the android app: 错误我在android应用中遇到以下错误:

IOException: Broken pipe IOException:管道损坏

You're making a mountain out of a molehill here. 您是在这里用积雪筑山。 Here's the simple way to do it: 这是简单的方法:

Socket clientSocket = serverSocket.accept();
byte[] receiveBuffer = new byte[Test.RECEIVE_BUFFER_LENGTH];
InputStream bufferedInputStream = new BufferedInputStream(clientSocket.getInputStream());
while (true) {
    int readBytes = bufferedInputStream.read(receiveBuffer);
    System.out.println("readBytes: " + readBytes); // Here I am getting 9 then -1.
    if (readBytes < 0) {
        break;
    }
    // Here you need to process `receiveBuffer[0..readBytes-1],
    //  and note that it may not contain a complete message,
    // so you may have to do more reading.
    // ...
    // after ending the processing send a reponse to the Android app.

    try {
        byte[]    answerBuffer = {0x00};
        clientSocket.getOutputStream().write(answerBuffer);
        System.out.println("Sent answer to the client");
    } catch (IOException e) {
            e.printStackTrace();
    }
}
clientSocket.close();

At present you are sending complete junk, because of all the pointless and incorrect messing around with ByteBuffers . 目前,由于所有与ByteBuffers毫无意义且不正确的混乱,您正在发送完全垃圾。

HOWEVER If this is correct: 但是,如果这是正确的:

<type> 0x00 0x00 0x00 <length> 0x00 0x00 0x00 [<data>]

you aren't sending it. 您没有发送。 A correct message of type 0 would surely look like this: 类型为0的正确消息肯定如下所示:

0x00 0x00 0x000 0x000 0x00 0x00 0x00

where the first three bytes are the type and the second three are the data length, evidently zero. 其中前三个字节是类型,后三个字节是数据长度,显然为零。 In code that would look like: 在如下代码中:

byte[] answerBuffer = {0,0,0,0,0,0};

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

相关问题 Nexus 2.10断管IOException - Nexus 2.10 broken pipe IOException Java NIO SocketChannel IOException:管道损坏 - Java NIO SocketChannel IOException: Broken pipe 仅在抛出IOException时检查管道是否损坏 - Check for Broken Pipe only when IOException is thrown IOException:运行Android应用程序时管道损坏 - IOException: Broken pipe when running Android application java.io.IOException: 管道损坏 - java.io.IOException: Broken pipe Cassandra节点故障,出现AssertionError,IOException(断开的管道)和OutOfMemoryError(堆) - Cassandra node down with AssertionError, IOException(broken pipe) and OutOfMemoryError(heap) PipedInputStream - 如何避免“java.io.IOException:Pipe broken” - PipedInputStream - How to avoid “java.io.IOException: Pipe broken” 避免日志充满 java.io.IOException: Broken pipe - Avoid logs full of java.io.IOException: Broken pipe 具有拍摄意图的Android FileProvider给出java.io.IOException:写入失败:EPIPE(管道损坏) - Android FileProvider with photo take intent gives a java.io.IOException: write failed: EPIPE (Broken pipe) 如何修复 java.io.IOException: write failed: EPIPE (Broken pipe) 当我扎根时? - How do I fix java.io.IOException: write failed: EPIPE (Broken pipe) when I am rooted?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM