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


C++ TCPClient::read方法代码示例

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


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

示例1: loop

void loop() {
  //low power
  WiFi.disconnect();
  System.sleep(D3, RISING, 4);
  if (digitalRead(D3)) {digitalWrite(D6,HIGH);delay(200);digitalWrite(D6,LOW);}

  while (!WiFi.ready()) {
    Particle.process();
    WiFi.connect();
    while(WiFi.connecting()){Particle.process();}
  }// while (!WiFi.ready())

  complete = false;

  //don't unsuccessfully persist beyond 10 secs, just go back to sleep
  lastTime = millis();
  while ((!complete) &&  (millis() - lastTime < 10000)){

  if (client.connect( server, serverPort)) {
    if (client.connected()) {
     sprintf(outmsg,"%c",clientmsg);
     out(outmsg);

    lastTime = millis();
    while ((!client.available()) && (millis() - lastTime < 5000)) { }

    lastTime = millis();
    while ((millis() - lastTime < 300)) {}//plays better with nodejs server?

 //now get confirmation from server that server received msg
    while (client.available()) {
      read_char = client.read();
      if(read_char == replymsg ) { //we got confirmation
        digitalWrite(D7,HIGH);delay(10);digitalWrite(D7,LOW);
        client.read();
        complete = true;
      }//if(read_char == replymsg )
    }//while (client.available())
  }//if (client.connected())
}//if (client.connect( server, serverPort))

  client.read();
  client.flush();
//  client.stop();//apparently unnecessary

}//while (!complete)

// prevent nodejs ECONNRESET, not necessay with another photon??
  lastTime = millis();
  while ((millis() - lastTime < 500)) {}//prevent nodejs ECONNRESET

delay(1);
}//loop
开发者ID:garthoff,项目名称:photon-simple-tcpclientserver,代码行数:53,代码来源:tcpclientSimple.cpp

示例2: loop

void loop() {
  if (client.connected()) {
    if (client.available()) {
      char pin = client.read() - '0' + D0;
      char level = client.read();
      if ('h' == level) {
        digitalWrite(pin, HIGH);
      } else {
        digitalWrite(pin, LOW);
      }
    }
  }
}
开发者ID:NOSVEEMOS2,项目名称:local-communication-example,代码行数:13,代码来源:firmware.cpp

示例3: in

void in(char *ptr, uint8_t timeout) {
  int pos = 0;
  unsigned long lastdata = millis();
  while ( client.available() || (millis()-lastdata < timeout)) {
    if (client.available()) {
      char c = client.read();
      lastdata = millis();
      ptr[pos] = c;
      pos++;
    }//if (client.available())
  }//while ( client.available() || (millis()-lastdata < timeout))
  client.read();
}//void in(char *ptr, uint8_t timeout)
开发者ID:YoitsuKamijo,项目名称:photon-simple-tcpclientserver,代码行数:13,代码来源:tcpclientFast.cpp

示例4: tcpKeepAlive

void tcpKeepAlive() {
    unsigned long now = millis();
    if (tcp.connected()) {
        if (now > kaTimer) {
            tcp.flush();
            tcpLog("ENQ"); // For logger view 
            tcp.print(ENQ); // Heartbeat signal
            kaWait = now + 500;
            kaWaiting = true;
            tcpKeepAliveReset();
        }
        if (kaWaiting && (kaWait > now)) {
            if (tcp.available()) {
                char read = tcp.read();
                if (read == ACK) {
                    // tcpLog("ACK");
                    kaWaiting = false;
                }
            }
        } else if (kaWaiting && kaWait < now) {
            tcpLog("Timed out");
            kaWaiting = false;
            tcp.flush();
            tcp.stop();
        }
    }
}
开发者ID:crstffr,项目名称:crstffr-spark-logger,代码行数:27,代码来源:firmware.cpp

示例5: loop

void loop() {

    char tmp[3];
    int charPosition = 0;

    // While there is data in the buffer, lets read it.
    while(client.available()) {

        char c = client.read();

        // Check if we reached the end of the data
        if(c == ',') {

            // use the accumulated value
            pData(tmp);

            // Reset
            tmp[0];
            charPosition = 0;

        } else {

            // Save char in to the temp array
            tmp[charPosition] = c;

            // Increment the position of the array
            charPosition++;

        }

    }

}
开发者ID:TheraPDX,项目名称:IoT-Raw-Sockets-Examples,代码行数:33,代码来源:tcpParticleClient.cpp

示例6: sendRequest

int sendRequest (byte* host, unsigned short port, char* response, unsigned short responseSize, boolean keepAlive)
{
  if (myClient.connected() || myClient.connect(host, port)) {
      uint32_t startTime = millis();

      myClient.write((const uint8_t *)mainbuffer, strlen(mainbuffer));
      myClient.flush();

      while(!myClient.available() && (millis() - startTime) < 5000){
          SPARK_WLAN_Loop();
      };

      while(myClient.available()) {
          readbytes = myClient.read((uint8_t*) response, responseSize);
          if (readbytes == -1) break;
      }

      myClient.flush();

      if(!keepAlive) {
        myClient.stop();
      }

  } else {
      // unable to connect
      return 1;
  }

  return 0;
}
开发者ID:Manu8D,项目名称:sparkHueLoopTest,代码行数:30,代码来源:http_client.cpp

示例7: loop

void loop()
{
    if (client.connected())
    {
        while (client.available())
        {
            in(inmsg,1000);
            myInStr =inmsg;
            if (myInStr.indexOf(clientmsg)  >= 0) {
                digitalWrite(D7, 1);
                delay(50);
                digitalWrite(D7, 0);
                sprintf(outmsg,"%c",replymsg);
                out(outmsg);
//THIS WORKS TOO
//                IPAddress myIP = WiFi.localIP();
//                sprintf(outmsg, "%d.%d.%d.%d,%d,2,%d,3,4/e", myIP[0], myIP[1], myIP[2], myIP[3],counter,simulate_temp);
//                out(outmsg); //but don't yet have a tcpclient that can handle such a response
            }//if (myInStr.indexOf("7")  >= 0)
        }//while (client.available())
    }//if (client.connected())
    client.read();
    client.flush();
//    client.stop(); //apparently unnecessary
    client = server.available();
 //keep connection alive long enough for client to receive reply
    lastTime = millis();
    while ( millis()-lastTime < 300) {}
}//loop
开发者ID:garthoff,项目名称:photon-simple-tcpclientserver,代码行数:29,代码来源:tcpserverSimple.cpp

示例8: loop

void loop() {

    if (clientWww.connected()) {

        int position = 0;
        int canI = 0;

        // echo all available bytes back to the client
        while (clientWww.available()) {

            char c = clientWww.read();
            char url[200];

            // Detect space
            if(c == 32) {

                canI = !canI;

            }

            // Save only data in between spaces
            if(canI) {

                // Discard spaces
                if(c != 32) {

                    url[position++] = c;

                }

            }

            // Detect end of the line
            if(c == 10) {

                // Null terminate our array
                url[position + 1] = 0;

                response(url);

                // Discard the rest of the header
                clientWww.flush();

            }

        }

    } else {

        // if no client is yet connected, check for a new connection
        clientWww = serverWww.available();

    }

}
开发者ID:TheraPDX,项目名称:IoT-Raw-Sockets-Examples,代码行数:55,代码来源:tcpDynamicPage.cpp

示例9: main

int main()
{
    signal(SIGPIPE, SIG_IGN);
    char buf[BUFSIZ];
    int clientCount = 0;
    try
    {
        TCPServer server(8001);
        int listenfd = server.getfd();
        Epoll epoll;
        // 将监听套接字注册到epoll
        epoll.addfd(server.getfd(), EPOLLIN, true);
        while (true)
        {
            int nReady = epoll.wait();
            for (int i = 0; i < nReady; ++i)
                // 如果是监听套接字发生了可读事件
                if (epoll.getEventOccurfd(i) == listenfd)
                {
                    int connectfd = accept(listenfd, NULL, NULL);
                    if (connectfd == -1)
                        err_exit("accept error");
                    cout << "accept success..." << endl;
                    cout << "clientCount = " << ++ clientCount << endl;
                    setUnBlock(connectfd, true);
                    epoll.addfd(connectfd, EPOLLIN, true);
                }
                else if (epoll.getEvents(i) & EPOLLIN)
                {
                    TCPClient *client = new TCPClient(epoll.getEventOccurfd(i));
                    memset(buf, 0, sizeof(buf));
                    if (client->read(buf, sizeof(buf)) == 0)
                    {
                        cerr << "client connect closed..." << endl;
                        // 将该套接字从epoll中移除
                        epoll.delfd(client->getfd());
                        delete client;
                        continue;
                    }
                    cout << buf;
                    client->write(buf);
                }
        }
    }
    catch (const SocketException &e)
    {
        cerr << e.what() << endl;
        err_exit("TCPServer error");
    }
    catch (const EpollException &e)
    {
        cerr << e.what() << endl;
        err_exit("Epoll error");
    }
}
开发者ID:Tachone,项目名称:LinuxPorgDemo,代码行数:55,代码来源:epollsrv.cpp

示例10: in

void in(char *ptr, uint8_t timeout) {
        int pos = 0;
        unsigned long lastTime = millis();
        while( client.available()==0 && millis()-lastTime<timeout) { //timeout
        }  //do nothing
        unsigned long lastdata = millis();
        while ( client.available() || (millis()-lastdata < 500)) {  //500 millisecond timeout
            if (client.available()) {
                char c = client.read();
                lastdata = millis();
                ptr[pos] = c;
                pos++;
            }
            if (pos >= 512 - 1)
            break;
        }
        ptr[pos] = '\0'; //end the char array
        while (client.available()) client.read(); // makeshift client.flush()
        client.flush();  //for safety
       lastdata = millis();
        while ( millis()-lastdata < 200) { }
}//void in(char *ptr, uint8_t timeout)
开发者ID:garthoff,项目名称:photon-simple-tcpclientserver,代码行数:22,代码来源:tcpserverSimple.cpp

示例11: loop

void loop() {

    if (client.connected()) {

        client.write("I am the server sending you a message.");

        while(client.available()) {

            char c = client.read();
            Serial.print(c);

        }

    } else {

        client = server.available();

    }

}
开发者ID:TheraPDX,项目名称:IoT-Raw-Sockets-Examples,代码行数:20,代码来源:tcpParticleServer.cpp

示例12: loop

void loop()
{
  // Now open your Serial Terminal, and hit any key to continue!
  if (Serial.available()) {
      char c = Serial.read();
  Serial.println(WiFi.localIP());
  Serial.println(WiFi.subnetMask());
  Serial.println(WiFi.gatewayIP());
  Serial.println(WiFi.SSID());
  }


    if (client.connected()) {
    // echo all available bytes back to the client
    while (client.available()) {
      server.write(client.read());
    }
  } else {
    // if no client is yet connected, check for a new connection
    client = server.available();
  }
}
开发者ID:Babbleshack,项目名称:firmware,代码行数:22,代码来源:echo_server.cpp

示例13: getData

/** Read data from client.
  @param data String to read the received data into.
  @param client TCPClient to get the data from.
*/
bool SparkWebSocketServer::getData(String &data, TCPClient &client)
{
    char buffer[packetLen];

    if(client.connected()) {
        while(client.available() == 0);

        for(int count = 0; client.available() > 0 && count < packetLen; count++) {
            buffer[count] = client.read();
        }

        if(packetHealth(buffer) != 0)
            return false;
        /*int failCount = 0;
        while(packetHealth(buffer) > 0) {
            // make room
            for(int i = 0; i < packetLen-1; i++)
                buffer[i] = buffer[i+1];

            while(client.available() == 0);

            buffer[packetLen-1] = client.read();

            failCount++;
        }

        if(failCount != 0)
            Serial.println(failCount);
        */

        for(int i = 0; i < dataLen; i++) {
            data += (char) (buffer[i+8] ^ buffer[4 + i % 4]);
        }
    }

    return true;
}
开发者ID:HagegeR,项目名称:L3D-spark,代码行数:41,代码来源:SparkWebSocketServer.cpp

示例14: loop

void loop() {



  if (client.connected()) {

    digitalWrite(D4,HIGH);
    digitalWrite(D0,HIGH);
    delay(200);
    digitalWrite(D4,LOW);
    digitalWrite(D0,LOW);

    while (client.available()) {

      digitalWrite(D4,HIGH);
      digitalWrite(D7,HIGH);
      delay(200);
      digitalWrite(D4,LOW);
      digitalWrite(D7,LOW);



      Serial.println();
      int val = client.read();
      Serial.println(val);

      netapp_ipconfig(&ipconfig);
      char connectedSSID[32];
      sprintf(connectedSSID, "%s", ipconfig.uaSSID);



      digitalWrite(D7,HIGH);
      delay(500);
      digitalWrite(D7,LOW);
      delay(500);

      long err = wlan_ioctl_get_scan_results(0, ucResults);
      int _numEntry = ((uint8_t) ucResults[3] << 24) | ((uint8_t) ucResults[2] << 16) | ((uint8_t) ucResults[1] << 8) | ((uint8_t) ucResults[0]);

      if (err == 0 && _numEntry > 0) {


        digitalWrite(D4,HIGH);
        delay(100);
        digitalWrite(D4,LOW);

        int _stat = ((uint8_t) ucResults[7] << 24) | ((uint8_t) ucResults[6] << 16) | ((uint8_t) ucResults[5] << 8) | ((uint8_t) ucResults[4]);
        bool _valid = (uint8_t) ucResults[8]  & 0x1;
        int _rssi = (uint8_t) ucResults[8] >> 1;
        int _mode = ((uint8_t) ucResults[9] | 0xC0) & 0x3;
        int _ssidlen = (uint8_t) ucResults[9] >> 2;

        char ssid[32];
        int idx = 0;
        while(idx < _ssidlen) {
          ssid[idx] = ucResults[idx+12];
          idx++;
        }
        ssid[_ssidlen] = (char) NULL;

        if (strcmp(connectedSSID, ssid) == 0){

          digitalWrite(D0,HIGH);
          delay(100);
          digitalWrite(D0,LOW);


          Serial.print("WiFi scan status: ");
          server.write("WiFi scan status: ");
          switch (_stat) {
            case 0:
            Serial.print("aged, ");
            server.write("aged, ");
            break;
            case 1:
            Serial.print("valid, ");
            server.write("valid, ");
            break;
            case 2:
            Serial.print("no results, ");
            server.write("no results, ");
            break;
          }

          Serial.print(_numEntry);
          Serial.print(" nets found. ");
          Serial.print(ssid);

          server.write(_numEntry);
          server.write(" nets found. ");
          server.write(ssid);

          if (_valid){
            Serial.print(" is valid, RSSI: ");
            server.write(" is valid, RSSI: ");
          }
          else{
            Serial.print("not valid, RSSI: ");
            server.write("not valid, RSSI: ");
//.........这里部分代码省略.........
开发者ID:firmread,项目名称:sparkPlay,代码行数:101,代码来源:application.cpp

示例15: loop

void loop()
{
    int debug = digitalRead(debug_tx);
    
    if(!digitalRead(waitpin_C)){
        // Skip mode, to ensure bootloader stays available
        char buffer[32];
        sprintf(buffer,"skip state: %d\r\n",state);
        Serial1.print(buffer);
        digitalWrite(led, HIGH);   // Turn ON the LED       
        delay(100);
        return;
    }else{
        digitalWrite(led, LOW);   // Turn OFF the LED  
    }

    delay(1);
    switch(state){
        case 0:
            // Waiting for next time
            wait-=10;
            if(wait<0){
                wait = 0;
                state = 1;
            }
            break;
        case 1:
            // Connecting
            if(debug) Serial1.println("connecting");
            if (client.connect(server, 80)){
                if(debug) Serial1.println("connected");
                state = 2;
            }else{
                if(debug) Serial1.println("connection failed state 1");
                wait = RETRY_INTERVAL;
                state = 0;
            }
            break;
        case 2:
            // Requesting
            tries = 0;
            store = 0;
            hash = 0;
            command_i = 0;
            for(int i=0; i<COMMAND_SIZE; i++) command[i] = 0;
            if(client.connected()){
                if(debug) client.println("GET // HTTP/1.0\r\n\r\n");
                wait = RESPONSE_INTERVAL;
                state = 3;
            }else{
                if(debug) Serial1.println("connection lost state 2");
                wait = RETRY_INTERVAL;
                state = 0;
            }
            break;
        case 3:
            // Receiving
            if(client.connected()){
                if (client.available() > 0) 
                {
                    // Print response to serial
                    char c = client.read();
                    if(debug) Serial1.print(c);
                    
                    // If last expected char found, quit reading
                    if(c =='>') hash = 1;
                    // If first char of data found, start storing the string
                    if(c =='<') store = 1;
                    if(store){
                        command[command_i++] = c;
                    }
                }else{
                    if(debug) Serial1.println("nd s3 ");  
                    delay(100);
                }
                // Quit reading
                if(hash){
                    Serial1.println();
                    state = 4;
                }
            }else{
                // We lost connection
                if(debug) Serial1.println("connection lost state 3");
                wait = RETRY_INTERVAL;
                state = 0;
            }
            break;
        case 4:
            // Disconnecting
            if(client.connected()){
                client.stop();
                if(debug) Serial1.println("connection closed state 4");
                wait = RENEW_INTERVAL;
                state = 5;
            }else{
                if(debug) Serial1.println("connection closed by server state 4");
                wait = RENEW_INTERVAL;
                state = 5; 
            }
            break;
//.........这里部分代码省略.........
开发者ID:Jeroen6,项目名称:SparkCore_ThermoWeer,代码行数:101,代码来源:main.cpp


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