Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetNetIf SimpleSocket 1.0 mbed
Example/echoserver.cpp@12:52170a866d4a, 2011-08-19 (annotated)
- Committer:
- yamaguch
- Date:
- Fri Aug 19 02:17:01 2011 +0000
- Revision:
- 12:52170a866d4a
- Parent:
- 11:04c0acd7f510
- Child:
- 13:1adb19edf716
broken (echo client/echo server)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yamaguch |
4:59056313fbfa | 1 | #include "mbed.h" |
yamaguch |
4:59056313fbfa | 2 | #include "EthernetNetIf.h" |
yamaguch |
4:59056313fbfa | 3 | #include "SimpleSocket.h" |
yamaguch |
4:59056313fbfa | 4 | |
yamaguch |
12:52170a866d4a | 5 | void blink(DigitalOut& led, Timer& timer, float interval = 1.0); |
yamaguch |
12:52170a866d4a | 6 | |
yamaguch |
8:b35559c14d17 | 7 | int echoserver(int port) { |
yamaguch |
12:52170a866d4a | 8 | printf("** echoserver **\n"); |
yamaguch |
12:52170a866d4a | 9 | |
yamaguch |
4:59056313fbfa | 10 | DigitalOut led1(LED1); |
yamaguch |
4:59056313fbfa | 11 | DigitalOut led2(LED2); |
yamaguch |
12:52170a866d4a | 12 | |
yamaguch |
8:b35559c14d17 | 13 | ServerSocket server(port); |
yamaguch |
4:59056313fbfa | 14 | |
yamaguch |
4:59056313fbfa | 15 | Timer timer; |
yamaguch |
4:59056313fbfa | 16 | timer.start(); |
yamaguch |
4:59056313fbfa | 17 | |
yamaguch |
4:59056313fbfa | 18 | while (true) { |
yamaguch |
12:52170a866d4a | 19 | if (ClientSocket *socket = server.accept()) { |
yamaguch |
12:52170a866d4a | 20 | while (socket->connected()) { |
yamaguch |
12:52170a866d4a | 21 | if (socket->available()) { |
yamaguch |
12:52170a866d4a | 22 | char buf[8]; |
yamaguch |
12:52170a866d4a | 23 | int len = socket->read(buf, sizeof(buf)); |
yamaguch |
12:52170a866d4a | 24 | if (len > 0) socket->write(buf, len); |
yamaguch |
12:52170a866d4a | 25 | blink(led1, timer); |
yamaguch |
4:59056313fbfa | 26 | } |
yamaguch |
4:59056313fbfa | 27 | } |
yamaguch |
4:59056313fbfa | 28 | } |
yamaguch |
12:52170a866d4a | 29 | blink(led2, timer); |
yamaguch |
12:52170a866d4a | 30 | } |
yamaguch |
12:52170a866d4a | 31 | } |
yamaguch |
12:52170a866d4a | 32 | |
yamaguch |
12:52170a866d4a | 33 | void blink(DigitalOut& led, Timer& timer, float interval) { |
yamaguch |
12:52170a866d4a | 34 | if (timer.read() > interval) { |
yamaguch |
12:52170a866d4a | 35 | timer.reset(); |
yamaguch |
12:52170a866d4a | 36 | timer.start(); |
yamaguch |
12:52170a866d4a | 37 | led = !led; |
yamaguch |
4:59056313fbfa | 38 | } |
yamaguch |
4:59056313fbfa | 39 | } |