0

如何保持WiFly屏蔽连接以从缓冲区发送或接收?

我需要让它与服务器保持连接状态,并等待服务器多次发送数据。

我已经测试过,但是看到它只能从服务器接收一次,然后它就不能再接收了,除非我关闭连接并重新连接。但我不想重新连接!

代码:

loop{
    Package sendbuf;  //Package is a structure for converting buffer to Package struct
    sendbuf.header = 0xaa;
    if (client.connect())
        logon(sendbuf,client); //Send message to server; request log in.

    while (client.connected()) {
        //=====================================//
        //      while receive buffer           //
        //=====================================//

        if (client.available()) {
            unsigned char rxBuffer[256] = {};
            delay(10);

            //=======Receive buffer process=======
            int index=-1;
            do{
                index++;
                rxBuffer[index] = client.read();

                if (rxBuffer[index] == 0xff)
                {
                  index--;
                }
            } while (client.available());

            Package* buf = (Package*)rxBuffer;
            int result = readBufferfromServer(buf);

            if (result == 2)  //result:2 mean log in is a success!
            {
                security(sendbuf, client); //Send security message to the server.
            }
            else
                if (result == 1)  //result:1 mean security checking is a success!
                {
                    //Do something
                }

        }// end of client avaliable
    }
4

0 回答 0