IOException:管道损坏

时间:2017-06-12 08:00:33

标签: java sockets

我正在推动与Android应用程序通信的服务器端应用程序。 Android应用 在最初与C ++服务器通信之前已经实现。现在我想用Java代码替换C ++服务器。 Android应用程序与服务器通信,以便通过读卡器中的卡进行身份验证。

验证协议包含应用程序和服务器之间要成功完成的几个通信步骤。

应用程序和服务器之间的

消息具有以下形式:

<type> 0x00 0x00 0x00 <length> 0x00 0x00 0x00 [<data>]
  1. 首先,应用程序发送类型1的请求以建立与读卡器中的SIM卡的连接。
  2. 然后服务器上的clientSocket发送一个类型0的响应,表示头已收到最后一条消息。
  3. 之后,服务器收到类型2的新请求,将SIM卡的ATR(应答休息)发送给应用程序。
  4. 服务器的clientSocket向应用程序发送类型为2的消息。                       。                       。                       。                       。                       。                       。                       。                       。                       。                       。                       。                       。                       
  5. 最后,我想关闭服务器端的clientSocket和serverSocket。

    我添加了重要代码:

    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()));
                }
    
            }
        }
    
    }
    

    输出:

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

    错误 我在Android应用程序中收到以下错误:

      

    IOException:管道损坏

1 个答案:

答案 0 :(得分:1)

你在这里从一个小山丘上建造一座山。这是一种简单的方法:

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();

目前你正在发送完整的垃圾,因为ByteBuffers的所有无意义和不正确的混乱。

HOWEVER 如果这是正确的:

<type> 0x00 0x00 0x00 <length> 0x00 0x00 0x00 [<data>]
你没有发送它。类型为0的正确消息肯定会是这样的:

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

其中前三个字节是类型,后三个是数据长度,显然为零。在代码中看起来像:

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