.

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPEchoClient by mbed official

Committer:
gerardo_carmona
Date:
Thu Nov 20 23:57:21 2014 +0000
Revision:
8:675fd43f4ee7
Parent:
7:a6f3768bc4b7
TCP Echo Client Example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:e8f884c94549 1 #include "mbed.h"
emilmont 1:a51d8ed156e6 2 #include "EthernetInterface.h"
emilmont 1:a51d8ed156e6 3
gerardo_carmona 8:675fd43f4ee7 4 const char* ECHO_SERVER_ADDRESS = "10.43.11.185";
gerardo_carmona 8:675fd43f4ee7 5 const int ECHO_SERVER_PORT = 50001;
emilmont 1:a51d8ed156e6 6
emilmont 1:a51d8ed156e6 7 int main() {
emilmont 1:a51d8ed156e6 8 EthernetInterface eth;
emilmont 1:a51d8ed156e6 9 eth.init(); //Use DHCP
emilmont 1:a51d8ed156e6 10 eth.connect();
emilmont 1:a51d8ed156e6 11 printf("IP Address is %s\n", eth.getIPAddress());
emilmont 1:a51d8ed156e6 12
emilmont 3:3fbf0efec25a 13 TCPSocketConnection socket;
emilmont 3:3fbf0efec25a 14 while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
emilmont 3:3fbf0efec25a 15 printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
emilmont 1:a51d8ed156e6 16 wait(1);
emilmont 1:a51d8ed156e6 17 }
emilmont 1:a51d8ed156e6 18
emilmont 1:a51d8ed156e6 19 char hello[] = "Hello World\n";
emilmont 3:3fbf0efec25a 20 socket.send_all(hello, sizeof(hello) - 1);
emilmont 1:a51d8ed156e6 21
emilmont 1:a51d8ed156e6 22 char buf[256];
emilmont 3:3fbf0efec25a 23 int n = socket.receive(buf, 256);
emilmont 2:563aa530f0dd 24 buf[n] = '\0';
emilmont 1:a51d8ed156e6 25 printf("%s", buf);
emilmont 1:a51d8ed156e6 26
emilmont 3:3fbf0efec25a 27 socket.close();
emilmont 1:a51d8ed156e6 28 eth.disconnect();
emilmont 1:a51d8ed156e6 29
emilmont 1:a51d8ed156e6 30 while(true) {}
emilmont 1:a51d8ed156e6 31 }