
Test network communications
Dependencies: EthernetInterface mbed-rtos mbed
Revision 4:f2789e665f4e, committed 2014-05-04
- Comitter:
- MalcolmNixon
- Date:
- Sun May 04 04:31:58 2014 +0000
- Parent:
- 3:1e86c29d1eac
- Child:
- 5:18415c556f04
- Commit message:
- Got threads working, but it's fragile as hell
Changed in this revision
--- a/main.cpp Sun May 04 03:05:22 2014 +0000 +++ b/main.cpp Sun May 04 04:31:58 2014 +0000 @@ -2,60 +2,58 @@ #include "Thread.h" #include "mbed.h" +// Configure the LEDs (off) +DigitalOut led1(LED1, 1); DigitalOut led2(LED2, 1); -// Blink function -void blink(const void *param) + +void ledThreadProc(const void *param) { - led2 = !led2; + // Toggle the Green LED + for (;;) + { + led2 = !led2; + Thread::wait(1000); + } } int main(void) { - // Create a digital output on LED1 - DigitalOut led1(LED1, 1); + Thread ledThread(ledThreadProc); - // Start the blink timer at 1 second interval - RtosTimer timer(blink); - timer.start(1000); - - // Construct the Ethernet interface + // 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 - TCPSocketConnection conn; - if (svr.accept(conn) == 0) + svr.accept(conn); + + // Manage the connection + for (;;) { - // Turn the TCP_NODELAY option on - int opt = 1; - conn.set_option(IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); - - // Manage the connection - for (;;) + // Read the input + char buf[32]; + int nr = conn.receive(buf, 32); + if (nr <= 0) { - // Read the input - char buf[128]; - int nr = conn.receive(buf, 128); - if (nr <= 0) - { - break; - } - - // Toggle the output on led1 - led1 = !led1; - - // Write the response twice - conn.send(buf, nr); - conn.send(buf, nr); + break; } + + // Toggle the output on led1 + led1 = !led1; + + // Write the response twice + conn.send(buf, nr); + conn.send(buf, nr); } - } -} \ No newline at end of file + } +}
--- a/mbed-src.lib Sun May 04 03:05:22 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed-src/#d57c40a064c8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun May 04 04:31:58 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776 \ No newline at end of file