[英]IOException: Broken pipe
我正在實現與Android應用程序通信的服務器端應用程序。 在最初與C ++ Server通信之前,已經實現了Android應用。 現在,我想用java代碼替換C ++服務器。 Android應用程序與服務器通信,以在讀卡器中通過其卡進行身份驗證。
身份驗證協議包含應用程序和要成功完成的服務器之間的服務器通信步驟。
應用程序和服務器之間的消息具有以下形式:
<type> 0x00 0x00 0x00 <length> 0x00 0x00 0x00 [<data>]
最后,我想在服務器端關閉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:管道損壞
您是在這里用積雪築山。 這是簡單的方法:
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
毫無意義且不正確的混亂,您正在發送完全垃圾。
但是,如果這是正確的:
<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};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.