nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "test_env.h"
00003 #include "EthernetInterface.h"
00004 
00005 namespace {
00006     const int ECHO_SERVER_PORT = 7;
00007     const int BUFFER_SIZE = 64;
00008 }
00009 
00010 int main (void) {
00011     MBED_HOSTTEST_TIMEOUT(20);
00012     MBED_HOSTTEST_SELECT(tcpecho_server_auto);
00013     MBED_HOSTTEST_DESCRIPTION(TCP echo server);
00014     MBED_HOSTTEST_START("NET_3");
00015 
00016     char buffer[BUFFER_SIZE] = {0};
00017     EthernetInterface eth;
00018     eth.init(); //Use DHCP
00019     eth.connect();
00020     printf("MBED: Server IP Address is %s:%d" NL, eth.getIPAddress(), ECHO_SERVER_PORT);
00021 
00022     TCPSocketServer server;
00023     server.bind(ECHO_SERVER_PORT);
00024     server.listen();
00025 
00026     while (true) {
00027         printf("MBED: Wait for new connection..." NL);
00028         TCPSocketConnection client;
00029         server.accept(client);
00030         client.set_blocking(false, 1500); // Timeout after (1.5)s
00031         printf("MBED: Connection from: %s" NL, client.get_address());
00032 
00033         while (true) {
00034             const int n = client.receive(buffer, sizeof(buffer));
00035             if (n <= 0) {
00036                 break;
00037             }
00038             const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE-1 : n;
00039             buffer[buffer_string_end_index] = '\0';
00040             client.send_all(buffer, n);
00041             if (n <= 0) {
00042                 break;
00043             }
00044         }
00045         client.close();
00046     }
00047 }