|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public void sendCameraCmdThread(byte[] cmd) {
if (cmd == null) return;
Log.i("TEST","====================================sendCameraCmd");
try {
socketClientThread.sendUrgentData();
socketClientThread.sendCameraCmdThread(cmd);
} catch (Exception e) {
//重新连接的代码
e.printStackTrace();
socketClientThread = new SocketClientThread();
socketClientThread.setByteArrCommand(cmd);
socketClientThread.start();
}
}
|
|
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
|
@Override
public void run() {
super.run();
Timer timer = new Timer();
try {
if (clientSocket == null) {
clientSocket = new Socket();
clientSocket.connect(new InetSocketAddress(IP,PORT),5000);
if (clientSocket!=null) {
clientSocket.setReceiveBufferSize(SOCKET_RECV_BUFFER_SIZE);
clientSocket.setSoTimeout(30*60*1000);
cameraOutputStream = clientSocket.getOutputStream();
cameraInputStream = clientSocket.getInputStream();
if (cameraInputStream!=null) {
try {
byte[] buffers = new byte[56];
int size = 0;
timer.schedule(new TimerTask() {
@Override
public void run() {
if(cmdArr!=null) {
for (int i = 0; i < cmdArr.length; i++) {
sendCameraCmdThread(cmdArr[i]);
}
}
}
}, 100);
Log.i("TEST","======================>start time");
while (clientSocket!=null&&(size = cameraInputStream.read(buffers))!= -1) {
Log.i("TEST", "===================> receive msg: " +Utils.bytesToHexString(buffers));
}
} catch (Exception e) {
}
} else {
Log.e("TEST","=================> cameraInputStream is null");
}
} else {
}
}
} catch (Exception e) {
} finally {
Log.i("TEST","===================>Client Close!");
if(timer!=null) {
timer.cancel();
}
if (cameraOutputStream!=null) {
try {
cameraOutputStream.close();
cameraOutputStream=null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (cameraInputStream!=null) {
try {
cameraInputStream.close();
cameraInputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (clientSocket!=null) {
try {
clientSocket.close();
clientSocket=null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|