我在程序中的服務器上正在運行,當cleint發送消息時,我首先發送一個1字節的ACK,其中1字節是我收到的msgType。將數據發送到套接字時發生ObjectOutputStream問題
我的程序執行流程是這樣的:
Socket connection = null;
connection = serverSocket.accept();
connection.setKeepAlive(true);
logger.info("server: connection received from " + connection.getInetAddress().getHostName());
out = new ObjectOutputStream(connection.getOutputStream());
.
.
switch(msgType) {
case 0:
// MSG_START
logger.info("Received MSG_START");
// send ACK
sendACK(out, 0);
logger.info("sent ACK for MSG_START");
break;
.
}
Then I have definition of sendAck function:
private static void sendACK(ObjectOutputStream out, int msgIntType) throws IOException {
// TODO Auto-generated method stub
byte[] msgType = new byte[1];
msgType[0] = (byte) (msgIntType & 0xFF);
logger.debug("Sending message to client: " + msgType.toString());
out.write(msgType);
out.flush();
logger.debug("Sending msg: " + Arrays.toString(msgType));
}
現在的問題是,在客戶端,客戶端時嘗試in.read(),它就會byteRead爲-1不是1
這裏可能是什麼問題?
由於提前, -JJ
是的,DataOutputStream是什麼需要在這裏...讓我們發送原始字節,或整數或浮動或任何... – 2011-04-25 16:34:19
我讀了很多人面臨DataInput/OutputStream的問題和一些字符串操作無法正常工作它。在我的情況下,客戶端是AIX和服務器是Linux,仍然DataOutput /輸入流工作正常嗎?我們主要發送/接收字節數組並將它們轉換爲ASCII字符串。 – user656189 2011-04-25 16:35:12
@ user656189我從來沒有遇到Data [Input | Output] Stream的問題,並且在Linux和AIX之間以這種方式進行通信時沒有問題。然而,就你而言,由於你似乎想在內部使用字符串,你不能簡單地發送ASCII字符串嗎?要發送字符串,您需要一個包裝在PrintWriter中的簡單OutputStreamWriter(例如)。只要你在兩邊使用相同的字符串編碼,這也應該工作得很好。 – alpian 2011-04-25 16:55:54