An Echo server as described in RFC862. Written as a learning exercise for using Donatien's network stack. Hopefully of some use to others to get started with socket programming.
main.cpp@0:c4e397ba6a9d, 2010-06-12 (annotated)
- Committer:
- darran
- Date:
- Sat Jun 12 19:05:52 2010 +0000
- Revision:
- 0:c4e397ba6a9d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
darran | 0:c4e397ba6a9d | 1 | /* |
darran | 0:c4e397ba6a9d | 2 | * Echo server |
darran | 0:c4e397ba6a9d | 3 | * Listens on TCP and UDP ports 7 for any incoming connections |
darran | 0:c4e397ba6a9d | 4 | * Re-transmits any incoming bytes |
darran | 0:c4e397ba6a9d | 5 | */ |
darran | 0:c4e397ba6a9d | 6 | |
darran | 0:c4e397ba6a9d | 7 | #include "mbed.h" |
darran | 0:c4e397ba6a9d | 8 | #include "EthernetNetIf.h" |
darran | 0:c4e397ba6a9d | 9 | |
darran | 0:c4e397ba6a9d | 10 | #include "EchoServer.h" |
darran | 0:c4e397ba6a9d | 11 | |
darran | 0:c4e397ba6a9d | 12 | // Our Ethernet interface |
darran | 0:c4e397ba6a9d | 13 | EthernetNetIf eth; |
darran | 0:c4e397ba6a9d | 14 | // Our Echo server |
darran | 0:c4e397ba6a9d | 15 | EchoServer server; |
darran | 0:c4e397ba6a9d | 16 | |
darran | 0:c4e397ba6a9d | 17 | /* |
darran | 0:c4e397ba6a9d | 18 | Function: main |
darran | 0:c4e397ba6a9d | 19 | |
darran | 0:c4e397ba6a9d | 20 | Sets up the Ethernet interface using DHCP, reports the assigned |
darran | 0:c4e397ba6a9d | 21 | IP address via serial, binds the Echo server to port 7 on |
darran | 0:c4e397ba6a9d | 22 | TCP and UDP and then sits in a loop calling Net::poll() to |
darran | 0:c4e397ba6a9d | 23 | keep the network stack doing its thing |
darran | 0:c4e397ba6a9d | 24 | */ |
darran | 0:c4e397ba6a9d | 25 | int main() { |
darran | 0:c4e397ba6a9d | 26 | printf("\r\nSetting up...\r\n"); |
darran | 0:c4e397ba6a9d | 27 | EthernetErr ethErr = eth.setup(); |
darran | 0:c4e397ba6a9d | 28 | if (ethErr) { |
darran | 0:c4e397ba6a9d | 29 | printf("Error %d in setup.\n", ethErr); |
darran | 0:c4e397ba6a9d | 30 | return -1; |
darran | 0:c4e397ba6a9d | 31 | } |
darran | 0:c4e397ba6a9d | 32 | IpAddr ip = eth.getIp(); |
darran | 0:c4e397ba6a9d | 33 | printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); |
darran | 0:c4e397ba6a9d | 34 | |
darran | 0:c4e397ba6a9d | 35 | server.bind(); |
darran | 0:c4e397ba6a9d | 36 | |
darran | 0:c4e397ba6a9d | 37 | printf("Entering while loop Net::poll()ing\r\n"); |
darran | 0:c4e397ba6a9d | 38 | while (1) { |
darran | 0:c4e397ba6a9d | 39 | Net::poll(); |
darran | 0:c4e397ba6a9d | 40 | } |
darran | 0:c4e397ba6a9d | 41 | } |