wifly tcp echo server example

Dependencies:   WiflyInterface mbed

Committer:
samux
Date:
Thu Dec 20 15:31:13 2012 +0000
Revision:
0:0710a5e21ef9
wifly tcp echo server example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:0710a5e21ef9 1 #include "mbed.h"
samux 0:0710a5e21ef9 2 #include "WiflyInterface.h"
samux 0:0710a5e21ef9 3
samux 0:0710a5e21ef9 4 #define ECHO_SERVER_PORT 7
samux 0:0710a5e21ef9 5
samux 0:0710a5e21ef9 6 /* wifly object where:
samux 0:0710a5e21ef9 7 * - p9 and p10 are for the serial communication
samux 0:0710a5e21ef9 8 * - p25 is for the reset pin
samux 0:0710a5e21ef9 9 * - p26 is for the connection status
samux 0:0710a5e21ef9 10 * - "mbed" is the ssid of the network
samux 0:0710a5e21ef9 11 * - "password" is the password
samux 0:0710a5e21ef9 12 * - WPA is the security
samux 0:0710a5e21ef9 13 */
samux 0:0710a5e21ef9 14 WiflyInterface wifly(p9, p10, p25, p26, "mbed", "password", WPA);
samux 0:0710a5e21ef9 15
samux 0:0710a5e21ef9 16 int main (void)
samux 0:0710a5e21ef9 17 {
samux 0:0710a5e21ef9 18 wifly.init(); // use DHCP
samux 0:0710a5e21ef9 19 while (!wifly.connect()); // join the network
samux 0:0710a5e21ef9 20 printf("IP Address is %s\n\r", wifly.getIPAddress());
samux 0:0710a5e21ef9 21
samux 0:0710a5e21ef9 22 TCPSocketServer server;
samux 0:0710a5e21ef9 23 server.bind(ECHO_SERVER_PORT);
samux 0:0710a5e21ef9 24 server.listen();
samux 0:0710a5e21ef9 25
samux 0:0710a5e21ef9 26 printf("\nWait for new connection...\n");
samux 0:0710a5e21ef9 27 TCPSocketConnection client;
samux 0:0710a5e21ef9 28 server.accept(client);
samux 0:0710a5e21ef9 29
samux 0:0710a5e21ef9 30 char buffer[256];
samux 0:0710a5e21ef9 31 while (true) {
samux 0:0710a5e21ef9 32 int n = client.receive(buffer, sizeof(buffer));
samux 0:0710a5e21ef9 33 if (n <= 0) continue;
samux 0:0710a5e21ef9 34 buffer[n] = 0;
samux 0:0710a5e21ef9 35
samux 0:0710a5e21ef9 36 client.send_all(buffer, n);
samux 0:0710a5e21ef9 37 }
samux 0:0710a5e21ef9 38 }