我遇到了我正在構建的服務器的問題。我將這個問題簡化爲一個簡單的Java服務器 - 客戶端對。我發送字節0到255.問題是從InputStream將128(0x80)到159(0x9F)的值作爲63(0x3F)接收。這是殺死我的二進制轉移。爲什麼會發生這種情況?Java的InputStream破壞信息,爲什麼?
此輸出我得到的,怎麼看值127後有一堆63年代:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 8
3 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 10
7 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 12
7 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
63 63 63 63 63 63 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 17
5 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 19
5 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 21
5 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 23
5 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 25
5
這是我的服務器代碼的基礎上,KnockKnockServer例如:
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
OutputStream out = clientSocket.getOutputStream();
InputStream in = clientSocket.getInputStream();
while (true)
{
int val = in.read();
if (val < 0)
continue;
System.out.print(val);
System.out.print(' ');
if (val == 255)
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
這是我客戶的代碼,基於EchoClient示例:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("localhost", 4444);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}
for (int i = 0; i < 256; i++)
out.print((char) i);
out.flush();
out.close();
in.close();
echoSocket.close();
}
}
運行此並告訴我,如果您獲得同樣奇怪的問題。請幫幫我。謝謝!
+1,而且他在另一端也使用了'BufferedReader',這也會有同樣的問題。在使用二進制數據時,不要使用任何'Reader'或'Writer'類〜 – 2012-02-25 21:59:43
即使使用PrintWriter,另一端也應該使用匹配的Reader。現在的代碼寫入char,並接收字節。 @ ErnestFriedman-Hill,他沒有在另一端使用BufferedReader。 – nos 2012-02-25 22:03:39
@mvds嘿謝謝! – marcoantap 2012-02-25 22:29:40