我正在实现与 Android 应用程序通信的服务器端应用程序。安卓应用程序 之前已经实现了原来与C++ Server的通信。现在我想用java代码替换C++服务器。 Android 应用程序与服务器通信,以通过读卡器中的卡对人员进行身份验证。
身份验证协议(protocol)包含应用程序和服务器之间要成功完成的通信的几个步骤。
应用程序和服务器之间的消息具有以下形式:
<type> 0x00 0x00 0x00 <length> 0x00 0x00 0x00 [<data>]
- 首先,应用发送类型 1 的请求以与读卡器中的 SIM 卡建立连接。
- 然后服务器上的 clientSocket 发送类型 0 的响应, header 已收到最后一条消息。
- 之后,服务器收到类型 2 的新请求,将 SIM 卡的 ATR(Answer To Rest)发送给应用。
- 服务器的clientSocket向应用程序发送类型2的消息。 。 。 。 。 。 。 。 。 。 。 。 。 .
最后我想关闭服务器端的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: Broken pipe
最佳答案
你这是小题大做了。这是执行此操作的简单方法:
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};
关于java - IOException : Broken pipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44494259/