Mohamad Nazrin Napiah / Mbed 2 deprecated TCPEchoClient

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPEchoClient by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 const char* ECHO_SERVER_ADDRESS = "192.168.137.1";
00005 const int ECHO_SERVER_PORT = 7;
00006 static const char*          mbedIp       = "192.168.137.2";  //IP
00007 static const char*          mbedMask     = "255.255.255.0";  // Mask
00008 static const char*          mbedGateway  = "192.168.137.1";    //Gateway
00009 int main() {
00010     EthernetInterface eth;
00011      eth.init(mbedIp,mbedMask,mbedGateway); 
00012     //eth.init(); //Use DHCP
00013     eth.connect();
00014     printf("\nClient IP Address is %s\n", eth.getIPAddress());
00015     
00016     // Connect to Server
00017     TCPSocketConnection socket;
00018     while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
00019         printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00020         wait(1);
00021     }
00022     printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
00023     
00024     // Send message to server
00025     char hello[] = "Hello World";
00026     printf("Sending  message to Server : '%s' \n",hello);
00027     socket.send_all(hello, sizeof(hello) - 1);
00028     
00029     // Receive message from server
00030     char buf[256];
00031     int n = socket.receive(buf, 256);
00032     buf[n] = '\0';
00033     printf("Received message from server: '%s'\n", buf);
00034     
00035     // Clean up
00036     socket.close();
00037     eth.disconnect();
00038     
00039     while(true) {}
00040 }