I'm试图在我的java方案中将一些简单的数据寄给TCP:
String data = "70798090999a9b9c9d9e9fa0a1a2";
ServerSocket srvr = new ServerSocket(1234);
Socket skt = srvr.accept();
OutputStream out = skt.getOutputStream();
out.write(hexStringToByteArray(data));
out.close();
skt.close();
srvr.close();
职能:
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
接收数据如下:
Socket skt = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
while (!in.ready()) {}
while (in.ready()) {
System.out.println(in.read()); // Read one line and output it
}
但是,我拿不出一个通常会增加人数的清单,而是会随意增加和减少:
121
196
234
244
246
245
250
249
251
252
8224
176
162
我在什么地方做过错?