私はまだプッシュサーバーに取り組んでいます!javax.crypto.cipherを使用して暗号化を正常に実装しました。これには、ソケットのストリームへのバイトの読み取り/書き込みが必要です。うまく送受信できます。暗号化は機能します。しかし、何も放送されていない場合、サーバーはOutOfMemoryExceptionをスローします。readBytes関数は次のとおりです。
public static byte[] readBytes() throws IOException {
int len = dis.readInt();
byte[] data = new byte[len];
if (len > 0) {
dis.readFully(data);
}
return data;
}
これを呼び出すコードは次のとおりです。
public String read() {
byte[] encrypted,decrypted = null;
try {
encrypted = readBytes();
if (encrypted.equals(new Byte[] {0})) return "";
if (encrypted.equals(null)) return null;
cipher.init(Cipher.DECRYPT_MODE, key);
decrypted = cipher.doFinal(encrypted);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String afterEncryption = new String(decrypted);
return afterEncryption;
}
そして、read()はwhileループで呼び出されます。
while (true&loggedin&((inputLine=read())!=null)) {
そして、スローされる例外は次のとおりです。
Exception in thread "ServerThread" java.lang.OutOfMemoryError: Java heap space
at ServerThread.readBytes(ServerThread.java:277)
at ServerThread.read(ServerThread.java:245)
at ServerThread.run(ServerThread.java:108)
行277は、バイト配列を宣言する行です。バイト配列を送信する関数は次のとおりです。
public static void sendBytes(byte[] myByteArray, int start, int len) throws IOException {
if (len < 0)
throw new IllegalArgumentException("Negative length not allowed");
if (start < 0 || start >= myByteArray.length)
throw new IndexOutOfBoundsException("Out of bounds: " + start);
if (len > 0) {
dos.writeInt(len);
dos.write(myByteArray, start, len);
}
}
sendBytesは、read()がブロックを停止した場合にのみ実行されます。これが、ループが機能するように設計されている方法です。
このコードがおなじみのように見える場合は、スタックオーバーフローで見つけたためです。
ハンドシェイクは問題なく機能しますが、送信が停止するとすぐに、約5秒後に例外が発生します。
あなたが私に与えることができるどんな助けにも感謝します。