溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android環(huán)境下使用SocketClient

發(fā)布時(shí)間:2020-07-09 06:45:24 來(lái)源:網(wǎng)絡(luò) 閱讀:1261 作者:stevelee_2012 欄目:移動(dòng)開(kāi)發(fā)

        最近對(duì)原來(lái)寫(xiě)的SocketClient代碼進(jìn)行優(yōu)化,從整體架構(gòu)到具體細(xì)節(jié),修改的地方比較多。今天有時(shí)間把SocketClient的相關(guān)知識(shí)整理一下。如果有錯(cuò)誤的地方,還望指正!!!


一、整體流程:

Android環(huán)境下使用SocketClient

   

 描述如下:

               1.  在Android環(huán)境下,SocketClient長(zhǎng)連接,需要使用service。

               2.  SocketManagerService是在APK啟動(dòng)時(shí)啟動(dòng)。

               3.  SocketManagerService啟動(dòng)時(shí)則SocketClientThread也啟動(dòng)。

               4.  View調(diào)用SocketManagerServicesendCmd方法發(fā)送命令。

               5.  如果SocketClient連接斷開(kāi),則重新建立連接,并且發(fā)送該命令。

               6. 狀態(tài)返回則通過(guò)自定義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ù),作為T(mén)CP緊急數(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表示沒(méi)有超時(shí)。該方法必須在阻塞方法之間調(diào)用。   

             個(gè)人理解:  

                    InputStream的read方法,在timeout的時(shí)間內(nèi)沒(méi)有接收到數(shù)據(jù),則超時(shí)。超時(shí)后read方法停止阻塞狀態(tài)。


三、問(wèn)題解答  :

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 void setCommandArr(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
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI