求助~如何在socket不断的情况下,收发16进制的数据?
手头有个项目,要求socket不断开,然后收发16进制的数据,只需要写客户端就够了,我已经实现了16进制数据的发送,可是如何接收?
private Socket socket;
private DataOutputStream ostream = null;
private BufferedReader istream = null;
String fs = "要发送的数据";
new Thread() {
public void run() {
String str = null;
try {
if (socket == null) {
socket = new Socket(ip, Integer.valueOf(dk));
ostream = new DataOutputStream(
socket.getOutputStream());
istream = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
这样写可以吗?socket是不会断,可是奇怪的是服务器目前是我发什么它回什么,但我之前用发送字符做测试的时候第一次发的东西总是会在第二次才能收到,这是为什么呢?
}
String arr[] = fs.split(" ");
byte[] b = new byte[arr.length];
for (int i = 0; i < arr.length; i++) {
b[i] = Hex.hexStringToBytes(arr[i])[0];
}
ostream.write(b);
ostream.flush();
str = istream.readLine();这里用什么东西接收?
} catch (Exception e) {
e.printStackTrace();
Log.e("e", e.toString());
try {
if (ostream != null) {
ostream.close();
}
if (istream != null) {
istream.close();
}
if (socket != null) {
socket.close();
socket = null;
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
};
}.start();