最近對(duì)原來寫的SocketClient代碼進(jìn)行優(yōu)化,從整體架構(gòu)到具體細(xì)節(jié),修改的地方比較多。今天有時(shí)間把SocketClient的相關(guān)知識(shí)整理一下。如果有錯(cuò)誤的地方,還望指正!!!
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),伊通企業(yè)網(wǎng)站建設(shè),伊通品牌網(wǎng)站建設(shè),網(wǎng)站定制,伊通網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,伊通網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。一、整體流程:

描述如下:
1. 在Android環(huán)境下,SocketClient長連接,需要使用service。
2. SocketManagerService是在APK啟動(dòng)時(shí)啟動(dòng)。
3. SocketManagerService啟動(dòng)時(shí)則SocketClientThread也啟動(dòng)。
4. View調(diào)用SocketManagerService的sendCmd方法發(fā)送命令。
5. 如果SocketClient連接斷開,則重新建立連接,并且發(fā)送該命令。
6. 狀態(tài)返回則通過自定義Listener實(shí)現(xiàn)。
二、預(yù)備知識(shí):
1. 判斷SocketClient是否與SocketServer連接:
1) . public void sendUrgentData(int value) throws IOException
源碼注釋:
Sends the given single byte data which is represented by the lowest octet of {@code value} as "TCP urgent data".
翻譯:
發(fā)送給定的代表最低字節(jié)碼值的單字節(jié)數(shù)據(jù),作為TCP緊急數(shù)據(jù) 。
個(gè)人理解:
該方法用于判斷SocketClient是否與SocketServer連接。
2.關(guān)于SocketClient的超時(shí)的理解:
1) . public void connect(SocketAddress remoteAddr, int timeout) throws IOException
源碼注釋:
Connects this socket to the remote host address and port number specified by the
{@code SocketAddress} object with the given timeout. This method will block indefinitely if the timeout is set to zero.
翻譯:
在有超時(shí)的情況下,socket連接指定地遠(yuǎn)程主機(jī)的地址和端口。如果timeout是0,則方法永遠(yuǎn)阻塞。
個(gè)人理解:
該方法的作用是SocketClient與SocketServer之間建立連接時(shí)的超時(shí)判斷。
2). public synchronized void setSoTimeout(int timeout) throws SocketException
源碼注釋:
Sets this socket's {@link SocketOptions#SO_TIMEOUT read timeout} in milliseconds.
Use 0 for no timeout. To take effect, this option must be set before the blocking method was called.
翻譯:
設(shè)置socketClient讀的超時(shí)時(shí)間。0表示沒有超時(shí)。該方法必須在阻塞方法之間調(diào)用。
個(gè)人理解:
InputStream的read方法,在timeout的時(shí)間內(nèi)沒有接收到數(shù)據(jù),則超時(shí)。超時(shí)后read方法停止阻塞狀態(tài)。
三、問題解答 :
1. 如何實(shí)現(xiàn)SocketClient的重新連接?
1). SocketClient設(shè)置setSoTime(int timeout) 當(dāng)超時(shí)后,則read停止阻塞,所以線程停止運(yùn)行。我代碼中timeout設(shè)置為30分鐘。
2). 在SocketManagerService中實(shí)現(xiàn)發(fā)送命令的方法,代碼如下:
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();
}
}2. SocketClient重新連接后,如何重發(fā)命令?
因?yàn)镾ocketClient重新連接,所以必須在SocketClient重新連接后才能重發(fā)命令。在線程中增加 public voidsetCommandArr(byte[] cmds) 方法。具體代碼如下:
@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();
}
}
}
}在SocketClient重新連接后,則使用Timer延時(shí)100毫秒后,則發(fā)送命令。這樣就能保證發(fā)送的命令成功。
附件:http://down.51cto.com/data/2368202另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享名稱:Android環(huán)境下使用SocketClient-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://chinadenli.net/article48/cepchp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、小程序開發(fā)、品牌網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站制作、靜態(tài)網(wǎng)站、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容