[英]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>]
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.