nkjnm
Dependencies: MAX44000 nexpaq_mdk
Fork of LED_Demo by
main.cpp
00001 #include "mbed.h" 00002 #include "test_env.h" 00003 #include "EthernetInterface.h" 00004 #include <algorithm> 00005 00006 namespace { 00007 const int BUFFER_SIZE = 64; 00008 const int MAX_ECHO_LOOPS = 1000; 00009 const char ASCII_MAX = '~' - ' '; 00010 00011 struct s_ip_address 00012 { 00013 int ip_1; 00014 int ip_2; 00015 int ip_3; 00016 int ip_4; 00017 }; 00018 } 00019 00020 char char_rand() { 00021 return (rand() % ASCII_MAX) + ' '; 00022 } 00023 00024 int main() { 00025 MBED_HOSTTEST_TIMEOUT(20); 00026 MBED_HOSTTEST_SELECT(tcpecho_client_auto); 00027 MBED_HOSTTEST_DESCRIPTION(TCP client echo loop); 00028 MBED_HOSTTEST_START("NET_13"); 00029 00030 char buffer[BUFFER_SIZE] = {0}; 00031 char out_buffer[BUFFER_SIZE] = {0}; 00032 s_ip_address ip_addr = {0, 0, 0, 0}; 00033 int port = 0; 00034 00035 printf("MBED: TCPCllient waiting for server IP and port...\r\n"); 00036 scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port); 00037 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); 00038 00039 EthernetInterface eth; 00040 eth.init(); //Use DHCP 00041 eth.connect(); 00042 00043 printf("MBED: TCPClient IP Address is %s\r\n", eth.getIPAddress()); 00044 sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4); 00045 00046 TCPSocketConnection socket; 00047 while (socket.connect(buffer, port) < 0) { 00048 printf("MBED: TCPCllient unable to connect to %s:%d\r\n", buffer, port); 00049 wait(1); 00050 } 00051 00052 // Test loop for multiple client connections 00053 bool result = true; 00054 int count_error = 0; 00055 for (int i = 0; i < MAX_ECHO_LOOPS; i++) { 00056 std::generate(out_buffer, out_buffer + BUFFER_SIZE, char_rand); 00057 socket.send_all(out_buffer, BUFFER_SIZE); 00058 00059 int n = socket.receive(buffer, BUFFER_SIZE); 00060 if (n > 0) 00061 { 00062 bool echoed = memcmp(out_buffer, buffer, BUFFER_SIZE) == 0; 00063 result = result && echoed; 00064 if (echoed == false) { 00065 count_error++; // Count error messages 00066 } 00067 } 00068 } 00069 00070 printf("MBED: Loop messages passed: %d / %d\r\n", MAX_ECHO_LOOPS - count_error, MAX_ECHO_LOOPS); 00071 00072 if (notify_completion_str(result, buffer)) { 00073 socket.send_all(buffer, strlen(buffer)); 00074 } 00075 socket.close(); 00076 eth.disconnect(); 00077 MBED_HOSTTEST_RESULT(result); 00078 }
Generated on Tue Jul 12 2022 17:03:48 by
1.7.2
