gs fan / Mbed 2 deprecated GSwifiInterface_TCPEchoClient

Dependencies:   GSwifiInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "GSwifiInterface.h"
00003 
00004 #define SEC  GSwifi::SEC_WPA_PSK
00005 #define SSID "SSID"
00006 #define PASS "PASSPHRASE"
00007 
00008 #define ECHO_SERVER_ADDRESS "192.168.1.101"
00009 #define ECHO_SERVER_PORT   10000
00010 
00011 int main() {
00012     GSwifiInterface gs(p13, p14, p12, P0_22, p20, NC);
00013     printf("TCP Echo Client...\n");
00014     gs.init(); //Use DHCP
00015     if (gs.connect(SEC, SSID, PASS)) return -1; // join the network
00016     printf("IP Address is %s\n", gs.getIPAddress());
00017     
00018     TCPSocketConnection socket;
00019     while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
00020         printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00021         wait(1);
00022     }
00023     
00024     char hello[] = "Hello World\n";
00025     socket.send_all(hello, sizeof(hello) - 1);
00026     
00027     char buf[256];
00028     int n = socket.receive(buf, 256);
00029     buf[n] = '\0';
00030     printf("%s", buf);
00031     
00032     socket.close();
00033     gs.disconnect();
00034     
00035     while(true) {}
00036 }