当前位置: 首页>>代码示例>>C++>>正文


C++ TcpServer::acceptClient方法代码示例

本文整理汇总了C++中TcpServer::acceptClient方法的典型用法代码示例。如果您正苦于以下问题:C++ TcpServer::acceptClient方法的具体用法?C++ TcpServer::acceptClient怎么用?C++ TcpServer::acceptClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TcpServer的用法示例。


在下文中一共展示了TcpServer::acceptClient方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: WiFiCmdRobot_main

int WiFiCmdRobot::WiFiCmdRobot_main() {
    String stringRead;
    int conx = 0;
    unsigned long timeout = 5; // 5s
    unsigned long start = 0;
    int ret=SUCCESS;

    // not specifically needed, we could go right to AVAILABLECLIENT
    // but this is a nice way to print to the serial monitor that we are 
    // actively listening.
    // Remember, this can have non-fatal falures, so check the status
    while (1) { 
        if(tcpServer.isListening(&status))
        {
            Serial.print("Listening on port: ");
            Serial.println(portServer, DEC);
            digitalWrite(Led_Yellow, HIGH);               // turn on led yellow
            break;
        }  
        else if(DNETcK::isStatusAnError(status))
        {
            Serial.println("Error Listening");
            tcpClient.close();
            tcpServer.close();
            return -1;
        }
    }

    // wait for a connection until timeout
    conx = 0;
    start = millis();
    while ((millis() - start < timeout*1000) && (conx == 0)) { 
        if((count = tcpServer.availableClients()) > 0)
        {
            Serial.print("Got ");
            Serial.print(count, DEC);
            Serial.println(" clients pending");
            conx = 1;
            // probably unneeded, but just to make sure we have
            // tcpClient in the  "just constructed" state
            tcpClient.close();             
        }
    }

    // accept the client 
    if((conx == 1) && (tcpServer.acceptClient(&tcpClient)))
    {
       Serial.println("Got a Connection");
       lcd.clear();
       lcd.print("Got a Connection");
       
       stringRead = "";
       while (tcpClient.isConnected())
       {
                if (tcpClient.available())
                {
                    char c = tcpClient.readByte();
                    if (c == '\n')
                    {
                          Serial.println (stringRead);
                          if (stringRead.startsWith("GET"))  
                          {
                                Serial.println("GET");
                                ret = Cmd (stringRead);
                                
                                if (ret == SUCCESS)
                                {
                                    ret = ReplyOK ();
                                }
                                else    
                                {
                                    cmd_GO[0] = 0; //reset GO command
                                    ret = ReplyKO ();
                                }
                                break;                             
 
                          }
                          else if (stringRead.startsWith("\r"))
                          {
                                // empty line => end
                                Serial.println("empty line => end");
                                break;
                          }                          
                          else
                          {
                                // no GET
                                Serial.println("no GET => ignore");
                          }
                          stringRead = "";              
                    }
                    else
                    {
                          stringRead += c;
                    }
                }
       } // end while            
    }
    else if((cmd_GO[0] == CMD_GO) && (cmd_GO[1] > t_GO+(uint16_t)timeout))  // GO ongoing
    {
       Serial.println("Continue command GO");
//.........这里部分代码省略.........
开发者ID:saildrones,项目名称:RobotChipKit,代码行数:101,代码来源:WiFiCmdRobot.cpp


注:本文中的TcpServer::acceptClient方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。