0
我在接收端即服務器有問題。它正在接收數據,我試圖將這些數據存儲在一個字節數組中。我正在做一個while循環,但它永遠不會終止,我不知道爲什麼。從Android設備發送哈希的字節數組到服務器套接字並存儲它
Android的代碼片段:
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] keyBytes = keystore.get("secretkey");
byte[] nonceBytes = nonce.getBytes("UTF-8");
outputStream.write(nonceBytes);
outputStream.write(keyBytes);
byte[] bytesToEncrypt = outputStream.toByteArray();
byte[] hashedBytes = digest.digest(bytesToEncrypt);
Socket clientSocket = new Socket(myserver, myport);
OutputStream outToServer = clientSocket.getOutputStream();
outToServer.write(hashedBytes);
}
服務器片斷:
try {
ServerSocket serverSocket = new ServerSocket(8311);
while(true) {
Socket connectionSocket = serverSocket.accept();
System.out.println("Receivied something..");
InputStream inFromClient = connectionSocket.getInputStream();
byte[] buffer = new byte[connectionSocket.getReceiveBufferSize()];
try {
System.out.println("Trying to store data in byte array");
int bytesRead = -1;
while((bytesRead = inFromClient.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
// This never prints
System.out.println("Done");
challengeReceived = baos.toByteArray();
}
catch(Exception e) {
e.printStackTrace();
}
}
它永遠不會打印 「完成」 這個實現。任何意見讚賞。當客戶機關閉連接
不能相信我錯過了。謝謝! – user3237636