我從一個套接字客戶端寫入的字節數組爲:如何確保我的服務器讀取客戶端寫入的內容?
byte[] arr = { 49, 49, 49, 49, 49};
InetAddress address = InetAddress.getByName(host);
connection = new Socket(address, port);
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
在接收端,在服務器上,我有:
byte[] msgType = new byte[5];
in = new BufferedInputStream(connection.getInputStream());
int bytesRead = in.read(msgType, 0, 5);
System.out.println("msg rcvd: " + msgType.toString());
在輸出我得到奇怪的字符串:
server: waiting for connection
server: connection received from localhost
server: Connection Successful
bytes read: 5
msg rcvd: ��w
如何確保獲得與我客戶端發送的字節相同的字節?
同意了,如果使用ObjectOutputStream或ObjectInputStream,則必須同時使用兩者。在這種情況下,你既不需要。 – MeBigFatGuy 2011-04-09 02:24:26
@meBigFatGuy - 我如何確保客戶端使用與服務器端相同的流。我正在處理服務器端,我被告知我將發送一個字節數組。我如何接收和打印該字節數組?在這種情況下,字節數組是文本/字符串的字節。 – user656189 2011-04-11 17:54:39
@ user656189:看看我最近編輯的例子。 – 2011-04-11 19:23:10