tree tree -4 years ago 91
Java Question

IOException: Broken pipe

I am implmenting server side application which communicates with Android app. The Android app
has been implemented before that originally communicate with the C++ Server. Now I want to replace the C++ server with java Code. The Android app communicates with the server to get the person authenticated by his card in the card reader.

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.

  2. Then the clientSocket on the server sends a reponse of type 0 that header has received the last message.

  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.

  4. the clientSocket of the server sends message of type 2 to the app.
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .



At the end I want to close the clientSocket and the serverSocket on the server side.

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:


IOException: Broken pipe

Answer Source

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.

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:

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};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download