Threads + Network Demo

Dependencies:   EthernetInterface mbed-rtos mbed-src

Fork of TestNetComm by Malcolm Nixon

main.cpp

Committer:
MalcolmNixon
Date:
2014-05-04
Revision:
4:f2789e665f4e
Parent:
3:1e86c29d1eac
Child:
5:18415c556f04

File content as of revision 4:f2789e665f4e:

#include "EthernetInterface.h"
#include "Thread.h"
#include "mbed.h"

// Configure the LEDs (off)
DigitalOut led1(LED1, 1);
DigitalOut led2(LED2, 1);
    

void ledThreadProc(const void *param)
{
    // Toggle the Green LED
    for (;;)
    {
        led2 = !led2;
        Thread::wait(1000);
    }
}

int main(void)
{
    Thread ledThread(ledThreadProc);

    // Start the network
    EthernetInterface eth;
    eth.init("192.168.5.100", "255.255.255.0", "0.0.0.0");
    eth.connect();

    // Construct the server
    TCPSocketConnection conn;
    TCPSocketServer svr;
    svr.bind(1234);
    svr.listen();

    for (;;)
    {
        // Accept the next connection
        svr.accept(conn);
            
        // Manage the connection
        for (;;)
        {
            // Read the input
            char buf[32];
            int nr = conn.receive(buf, 32);
            if (nr <= 0)
            {
                break;
            }
            
            // Toggle the output on led1
            led1 = !led1;
            
            // Write the response twice
            conn.send(buf, nr);
            conn.send(buf, nr);
        }
    }
}