本文整理汇总了C++中TCPClient::connected方法的典型用法代码示例。如果您正苦于以下问题:C++ TCPClient::connected方法的具体用法?C++ TCPClient::connected怎么用?C++ TCPClient::connected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TCPClient
的用法示例。
在下文中一共展示了TCPClient::connected方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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
示例2: 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;
}
示例3: 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();
}
}
}
示例4: loop
void loop() {
delay(1);
if (!WiFi.ready()) {
Particle.process();
WiFi.connect();
while(WiFi.connecting()) {Particle.process();}
}// if (!WiFi.ready())
else
{
complete = false;
lastTime = millis();
while ((!complete) && (millis() - lastTime < 10000)){
if (client.connect( server, serverPort)) {
if (client.connected()) {
out(clientmsg);
lastTime = millis();
while ((!client.available()) && (millis() - lastTime < 10000)) {Particle.process();}//wait for response
in(inmsg,10);//10 pure trial and error
myInStr =inmsg;
if (myInStr.indexOf(replymsg) >= 0) {
digitalWrite(D7, 1); // Flashes the LED
lastTime = millis();
while ( millis()-lastTime < 5) { } //5
digitalWrite(D7, 0); // Flashes the LED
complete = true;
}//if (myInStr.indexOf(replymsg) >= 0)
}//if (client.connected())
}//if (client.connect( server, serverPort))
}//while (!complete)
}//else
}//loop
示例5: tcpLog
int tcpLog(String message) {
if (tcp.connected()) {
tcp.print(STX + Spark.deviceID() + ETX + message + EOT);
delay(250); // try not to flood socket with too many writes
return 1;
} else {
return -1;
}
}
示例6: loop
void loop() {
if (client.connected()) {
Firmata.sendString("Hello World!");
delay(1000);
} else {
Serial.println("Disconnected.");
client.stop();
while(1) {}; // loop forever
}
}
示例7: 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();
}
}
示例8: 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
示例9: 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);
}
}
}
}
示例10: 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();
}
}
示例11: 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();
}
}
示例12: 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;
}
示例13: 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: ");
//.........这里部分代码省略.........
示例14: millis
char * http_get(char const * hostname, String path) {
int i = 0;
int j = 0;
int k = 0;
bool printOnce = false;
if (client.connect(hostname, 80)) {
client.print("GET ");
client.print(path);
client.println(" HTTP/1.1");
client.print("HOST: ");
client.println(hostname);
client.println("Connection: close");
client.println();
client.flush();
} else {
if(DEBUG_SERIAL) Serial.println("\r\n\r\nConnection Failed!");
client.flush();
client.stop();
return NULL;
}
// wait 5 seconds or less for the host to respond
uint32_t startTime = millis();
while(!client.available() && (millis() - startTime) < 5000);
if(DEBUG_SERIAL) Serial.println("\r\n\r\nREADING HOST DATA......");
uint32_t lastRead = millis();
// If the host doesn't close it's connection, we'll timeout in 10 seconds.
while (client.connected() && (millis() - lastRead) < 10000) {
while (client.available()) {
char c = client.read();
/*
if(DEBUG_SERIAL) {
Serial.print(c);
if(i++ > 100) {
delay(5);
i = 0;
}
}
*/
if(c == -1) {
Serial.print("\r\n\r\nERROR......\r\n\r\n");
client.flush();
client.stop();
}
if(j++ >= SKIP_CHARS) { // don't buffer the first X bytes to save memory
if(DEBUG_SERIAL && !printOnce) {
Serial.print("\r\n\r\nSAVING......\r\n\r\n");
printOnce = true;
}
buffer[k++] = c; // save character to buffer
Serial.print(c);
delayMicroseconds(100);
if(k >= BUFFER_SIZE_MAX) { // if we reach the end of our buffer, just bail.
Serial.print("\r\n\r\nOUT OF BUFFER SPACE......\r\n\r\n");
client.flush();
client.stop();
}
}
// as long as we're reading data, reset the lastRead time.
lastRead = millis();
} // END while(client.available())
} // END while(client.connected())
client.flush();
client.stop();
if(DEBUG_SERIAL) {
Serial.print("\r\nCHARACTERS RECEIVED: ");
Serial.println(SKIP_CHARS + k);
}
return buffer;
}
示例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;
//.........这里部分代码省略.........