int main() { char buffer[BUFFER_SIZE] = {0}; char out_buffer[BUFFER_SIZE] = {0}; s_ip_address ip_addr = {0, 0, 0, 0}; int port = 0; printf("MBED: TCPCllient waiting for server IP and port...\r\n"); scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port); printf("MBED: Address received: %d.%d.%d.%d:%d\r\n", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port); EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("MBED: TCPClient IP Address is %s\r\n", eth.getIPAddress()); sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4); TCPSocketConnection socket; while (socket.connect(buffer, port) < 0) { printf("MBED: TCPCllient unable to connect to %s:%d\r\n", buffer, port); wait(1); } // Test loop for multiple client connections bool result = true; int count_error = 0; for (int i = 0; i < MAX_ECHO_LOOPS; i++) { std::generate(out_buffer, out_buffer + BUFFER_SIZE, char_rand); socket.send_all(out_buffer, BUFFER_SIZE); int n = socket.receive(buffer, BUFFER_SIZE); if (n > 0) { bool echoed = memcmp(out_buffer, buffer, BUFFER_SIZE) == 0; result = result && echoed; if (echoed == false) { count_error++; // Count error messages } } } printf("MBED: Loop messages passed: %d / %d\r\n", MAX_ECHO_LOOPS - count_error, MAX_ECHO_LOOPS); if (notify_completion_str(result, buffer)) { socket.send_all(buffer, strlen(buffer)); } socket.close(); eth.disconnect(); notify_completion(result); return 0; }
char putStream(char* streamName, char* json) { int json_length; char http_cmd[512]; char buffer[1024]; int ret; sock.connect(APIHOST, 80); json_length = strlen(json); sprintf(http_cmd, "PUT /v1/feeds/%s/streams/%s HTTP/1.0\r\nX-M2X-KEY: %s\r\nContent-length: %d\r\nContent-type: application/json\r\n\r\n%s\r\n", FEEDID, streamName, APIKEY, json_length, json); //pc.printf(http_cmd); sock.send_all(http_cmd, sizeof(http_cmd)-1); ret = sock.receive(buffer, sizeof(buffer)-1); if (ret <= 0) return(0); buffer[ret] = '\0'; if (strncmp(buffer, "HTTP/", 5) == 0) { ret = atoi(&buffer[9]); } //pc.printf("Received %d chars from server:\r\n%s\r\n", ret, buffer); strcpy(json, buffer); sock.close(); return ret; }
char getLocation(char* json) { char http_cmd[512]; char buffer[1024]; int ret; sock.connect(APIHOST, APIPORT); sprintf(http_cmd, "GET /v1/feeds/%s/location HTTP/1.0\r\nX-M2X-KEY: %s\r\n\r\n", FEEDID, APIKEY); //pc.printf(http_cmd); sock.send_all(http_cmd, sizeof(http_cmd)-1); ret = sock.receive(buffer, sizeof(buffer)-1); if (ret <= 0) return(0); buffer[ret] = '\0'; if (strncmp(buffer, "HTTP/", 5) == 0) { ret = atoi(&buffer[9]); } //pc.printf("Received %d chars from server:\r\n%s\r\n", ret, buffer); strcpy(json, buffer); sock.close(); return ret; }
int main(void) { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("Server IP Address is %s:%d\n", eth.getIPAddress(), ECHO_SERVER_PORT); Thread UdpServerTask(udp_server_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25); Thread UdpClientTask(udp_client_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25); // Control TCP server to get mbed statistics { const int TELNET_SERVER_PORT = 23; const int BUFFER_SIZE = 256; TCPSocketServer server; server.bind(TELNET_SERVER_PORT); server.listen(); while (true) { printf("Wait for new connection...\n"); TCPSocketConnection client; server.accept(client); client.set_blocking(false, 1500); // Timeout after (1.5)s printf("Connection from: %s\n", client.get_address()); char buffer[BUFFER_SIZE] = { 0 }; while (true) { int n = client.receive(buffer, sizeof(buffer)); //if (n <= 0) break; if (n > 0) { printf("Recv %d chars\r\n", n); const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE - 1 : n; buffer[buffer_string_end_index] = '\0'; // client.send_all(buffer, strlen(buffer)); if (strncmp(buffer, "stat", 4) == 0) { sprintf(buffer, "received_packets %d\nforwarded_packets %d\nmax_queue_len %d", received_packets, forwarded_packets, max_queue_len); client.send_all(buffer, strlen(buffer)); // printf("%s", buffer); } } //if (n <= 0) break; } client.close(); } } }