wireless
Dependencies: EthernetInterface mbed-rtos mbed
Fork of TCPEchoServer by
main.cpp@6:30f62685b255, 2014-05-16 (annotated)
- Committer:
- avnisha
- Date:
- Fri May 16 19:34:28 2014 +0000
- Revision:
- 6:30f62685b255
- Parent:
- 3:36fd3cfad85a
timeout 100 secs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:38cbb854d85f | 1 | #include "mbed.h" |
emilmont | 1:5cebe0e38cd2 | 2 | #include "EthernetInterface.h" |
avnisha | 6:30f62685b255 | 3 | #include <cctype> |
emilmont | 1:5cebe0e38cd2 | 4 | |
emilmont | 3:36fd3cfad85a | 5 | #define ECHO_SERVER_PORT 7 |
emilmont | 3:36fd3cfad85a | 6 | |
emilmont | 1:5cebe0e38cd2 | 7 | int main (void) { |
emilmont | 1:5cebe0e38cd2 | 8 | EthernetInterface eth; |
emilmont | 1:5cebe0e38cd2 | 9 | eth.init(); //Use DHCP |
emilmont | 1:5cebe0e38cd2 | 10 | eth.connect(); |
emilmont | 1:5cebe0e38cd2 | 11 | printf("IP Address is %s\n", eth.getIPAddress()); |
emilmont | 1:5cebe0e38cd2 | 12 | |
emilmont | 1:5cebe0e38cd2 | 13 | TCPSocketServer server; |
emilmont | 3:36fd3cfad85a | 14 | server.bind(ECHO_SERVER_PORT); |
emilmont | 3:36fd3cfad85a | 15 | server.listen(); |
emilmont | 1:5cebe0e38cd2 | 16 | |
emilmont | 1:5cebe0e38cd2 | 17 | while (true) { |
emilmont | 1:5cebe0e38cd2 | 18 | printf("\nWait for new connection...\n"); |
emilmont | 1:5cebe0e38cd2 | 19 | TCPSocketConnection client; |
emilmont | 1:5cebe0e38cd2 | 20 | server.accept(client); |
avnisha | 6:30f62685b255 | 21 | client.set_blocking(false, 100000); // NB Timeout |
emilmont | 1:5cebe0e38cd2 | 22 | |
emilmont | 1:5cebe0e38cd2 | 23 | printf("Connection from: %s\n", client.get_address()); |
emilmont | 1:5cebe0e38cd2 | 24 | char buffer[256]; |
emilmont | 1:5cebe0e38cd2 | 25 | while (true) { |
emilmont | 3:36fd3cfad85a | 26 | int n = client.receive(buffer, sizeof(buffer)); |
emilmont | 1:5cebe0e38cd2 | 27 | if (n <= 0) break; |
emilmont | 1:5cebe0e38cd2 | 28 | |
avnisha | 6:30f62685b255 | 29 | char *p = buffer; |
avnisha | 6:30f62685b255 | 30 | while (*p) { |
avnisha | 6:30f62685b255 | 31 | *p = toupper(*p); |
avnisha | 6:30f62685b255 | 32 | p++; |
avnisha | 6:30f62685b255 | 33 | } |
emilmont | 2:ec5ae99791da | 34 | client.send_all(buffer, n); |
emilmont | 1:5cebe0e38cd2 | 35 | if (n <= 0) break; |
emilmont | 1:5cebe0e38cd2 | 36 | } |
emilmont | 3:36fd3cfad85a | 37 | |
emilmont | 1:5cebe0e38cd2 | 38 | client.close(); |
emilmont | 1:5cebe0e38cd2 | 39 | } |
emilmont | 1:5cebe0e38cd2 | 40 | } |