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


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

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


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

示例1: main

int main()
{
    TcpServer server;

    if (server.listen(2000) != AbstractSocket::Done)
    {
        perror("Error :");
        return -1;
    }

    TcpSocket* s = server.accept();

    if (s == NULL)
    {
        cout << "Error: cannot accept conection" << endl;
        return -1;
    }

    cout << "Connected client" << endl;
    int i = 4;
    i++;
    cout << "Remote host : " << s->getRemotePort() << endl;
    cout << s->getLocalPort() << endl;
    cout << server.getRemoteAddress().toString() << endl;
    cout << s->getRemoteAddress().toString() << endl;
    s->sendInt32(10);
    sleep(2);
    s->sendString("hello");
    s->sendCharArray("world", 5);
    Frame f;
    f << "This is a frame " << 666 << '\n';

    if (s->sendFrame(f) != AbstractSocket::Done)
        perror("error on send frame");

    delete s;
    server.close();
    s->close();
    return 1;
}
开发者ID:AnXi-TieGuanYin-Tea,项目名称:CPPWrappers,代码行数:40,代码来源:Server.cpp

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