3 #include "EthernetInterface.h"
7 const int BUFFER_SIZE
= 64;
8 const int MAX_ECHO_LOOPS
= 1000;
9 const char ASCII_MAX
= '~' - ' ';
21 return (rand() % ASCII_MAX
) + ' ';
25 MBED_HOSTTEST_TIMEOUT(20);
26 MBED_HOSTTEST_SELECT(tcpecho_client_auto
);
27 MBED_HOSTTEST_DESCRIPTION(TCP client echo loop
);
28 MBED_HOSTTEST_START("NET_13");
30 char buffer
[BUFFER_SIZE
] = {0};
31 char out_buffer
[BUFFER_SIZE
] = {0};
32 s_ip_address ip_addr
= {0, 0, 0, 0};
35 printf("MBED: TCPCllient waiting for server IP and port...\r\n");
36 scanf("%d.%d.%d.%d:%d", &ip_addr
.ip_1
, &ip_addr
.ip_2
, &ip_addr
.ip_3
, &ip_addr
.ip_4
, &port
);
37 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
);
39 EthernetInterface eth
;
40 eth
.init(); //Use DHCP
43 printf("MBED: TCPClient IP Address is %s\r\n", eth
.getIPAddress());
44 sprintf(buffer
, "%d.%d.%d.%d", ip_addr
.ip_1
, ip_addr
.ip_2
, ip_addr
.ip_3
, ip_addr
.ip_4
);
46 TCPSocketConnection socket
;
47 while (socket
.connect(buffer
, port
) < 0) {
48 printf("MBED: TCPCllient unable to connect to %s:%d\r\n", buffer
, port
);
52 // Test loop for multiple client connections
55 for (int i
= 0; i
< MAX_ECHO_LOOPS
; i
++) {
56 std::generate(out_buffer
, out_buffer
+ BUFFER_SIZE
, char_rand
);
57 socket
.send_all(out_buffer
, BUFFER_SIZE
);
59 int n
= socket
.receive(buffer
, BUFFER_SIZE
);
62 bool echoed
= memcmp(out_buffer
, buffer
, BUFFER_SIZE
) == 0;
63 result
= result
&& echoed
;
64 if (echoed
== false) {
65 count_error
++; // Count error messages
70 printf("MBED: Loop messages passed: %d / %d\r\n", MAX_ECHO_LOOPS
- count_error
, MAX_ECHO_LOOPS
);
72 if (notify_completion_str(result
, buffer
)) {
73 socket
.send_all(buffer
, strlen(buffer
));
77 MBED_HOSTTEST_RESULT(result
);