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; }
void response(char* url) { // default page is the error page const char* page = error; // Get the length of our URL and remove the null termination int urlLen = strlen(url) - 1; // HOME: check only for the length of the URL, since the home page // is represented by a forward slash / if(urlLen == 1) { page = home; } // MORE: look for the word if(memcmp(url, "/more", 5) == 0) { page = more; } // HOME: look for the word if(memcmp(url, "/author", 6) == 0) { page = author; } // Inform the browser that everything is ok clientWww.write("HTTP/1.1 200 OK\n\r"); // Tell the encoding that we are using clientWww.write("Content-Type: text/html; charset=ascii\n\r"); // Separate the header from the body with one empty line clientWww.write("\n\r"); // Send our content clientWww.write(page); // Close the connection clientWww.stop(); }
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"); } }
/** Send a string to a client. */ void SparkWebSocketServer::sendEncodedData(char *str, TCPClient &client) { int size = strlen(str); if(!client) return; // NOTE: no support for > 16-bit sized messages if(size > 125) { uint8_t buffer[size+4]; strcpy((char*)(buffer+4), str); buffer[0] = 0x81; // string type buffer[1] = 126; buffer[2] = size >> 8; buffer[3] = size & 0xFF; client.write(buffer, sizeof(buffer)); } else {
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(); } }
void out(const char *s) {client.write( (const uint8_t*)s, strlen(s) );}
void loop () { if (!initialised && !connected && (lastCheck + 5000 < millis())) { if (WiFi.ready()) { connect("www.apple.com", 80); if (connected) { client.println("GET /library/test/success.html HTTP/1.1"); client.println("Host: www.apple.com"); client.println(); const char *success = "Success"; unsigned int successLocation = 0; while (true) { if (client.available()) { int chr = client.read(); if (success[successLocation] == chr) { successLocation ++; if (successLocation == strlen(success)) { initialised = true; Spark.connect(); Spark.function("digitalread", tinkerDigitalRead); Spark.function("digitalwrite", tinkerDigitalWrite); Spark.function("analogread", tinkerAnalogRead); Spark.function("analogwrite", tinkerAnalogWrite); client.stop(); break; } } else { successLocation = 0; } } else { delay(2); } } } } else { return; } } if (Serial.available()) { if (connected) { int bytes = Serial.available(); for (int i=0; i<bytes; i++) { client.write(readSerial()); } } else { int cmd = readSerial(); switch (cmd) { case 'i': Serial.println("Ready"); break; case 'c': { char* host; char* stringPort; readWord(); // junk space host = readWord(); stringPort = readWord(); connect(host, atol(stringPort)); free(host); free(stringPort); break; } case 'w': { char* ssid; char* password; readWord(); ssid = readWord(); password = readWord(); Serial.print("Wifi credentials set to SSID '"); Serial.print(ssid); Serial.print("' and password '"); Serial.print(password); Serial.println("'"); WiFi.setCredentials(ssid, password); break; } default: Serial.print("Don't know command: "); Serial.write(cmd); Serial.println(); break; } } } if (connected) { int bytes = client.available(); for (int i=0; i<bytes; i++) { Serial.write(client.read()); } if (!client.connected()) { Serial.println(); Serial.println("Disconnecting"); client.stop(); connected = false; } } }