asdasdasdasd
Dependencies: Audio EthernetInterface mbed-rtos mbed
Fork of FRDM_TCP_v4_copy by
main.cpp@6:dd8d1cf1cb9c, 2017-02-14 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Feb 14 16:43:07 2017 +0000
- Revision:
- 6:dd8d1cf1cb9c
- Parent:
- 3:3fbf0efec25a
- Child:
- 7:88de3c8d25cb
Update libraries. Make example more verbose.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 6:dd8d1cf1cb9c | 1 | 1 |
mbed_official | 6:dd8d1cf1cb9c | 2 | 2 |
mbed_official | 6:dd8d1cf1cb9c | 3 | 3 |
mbed_official | 6:dd8d1cf1cb9c | 4 | 4 |
mbed_official | 6:dd8d1cf1cb9c | 5 | 5 |
mbed_official | 6:dd8d1cf1cb9c | 6 | 6 |
mbed_official | 6:dd8d1cf1cb9c | 7 | 7 |
mbed_official | 6:dd8d1cf1cb9c | 8 | 8 |
mbed_official | 6:dd8d1cf1cb9c | 9 | 9 |
mbed_official | 6:dd8d1cf1cb9c | 10 | 10 |
mbed_official | 6:dd8d1cf1cb9c | 11 | 11 |
mbed_official | 6:dd8d1cf1cb9c | 12 | 12 |
mbed_official | 6:dd8d1cf1cb9c | 13 | 13 |
mbed_official | 6:dd8d1cf1cb9c | 14 | 14 |
mbed_official | 6:dd8d1cf1cb9c | 15 | 15 |
mbed_official | 6:dd8d1cf1cb9c | 16 | 16 |
mbed_official | 6:dd8d1cf1cb9c | 17 | 17 |
mbed_official | 6:dd8d1cf1cb9c | 18 | 18 |
mbed_official | 6:dd8d1cf1cb9c | 19 | 19 |
mbed_official | 6:dd8d1cf1cb9c | 20 | 20 |
mbed_official | 6:dd8d1cf1cb9c | 21 | 21 |
mbed_official | 6:dd8d1cf1cb9c | 22 | 22 |
mbed_official | 6:dd8d1cf1cb9c | 23 | 23 |
mbed_official | 6:dd8d1cf1cb9c | 24 | 24 |
mbed_official | 6:dd8d1cf1cb9c | 25 | 25 |
mbed_official | 6:dd8d1cf1cb9c | 26 | 26 |
mbed_official | 6:dd8d1cf1cb9c | 27 | 27 |
mbed_official | 6:dd8d1cf1cb9c | 28 | 28 |
mbed_official | 6:dd8d1cf1cb9c | 29 | 29 |
mbed_official | 6:dd8d1cf1cb9c | 30 | 30 |
mbed_official | 6:dd8d1cf1cb9c | 31 | 31 |
mbed_official | 6:dd8d1cf1cb9c | 32 | 32 |
mbed_official | 6:dd8d1cf1cb9c | 33 | 33 |
mbed_official | 6:dd8d1cf1cb9c | 34 | 34 |
mbed_official | 6:dd8d1cf1cb9c | 35 | 35 |
mbed_official | 6:dd8d1cf1cb9c | 36 | 36 |
mbed_official | 6:dd8d1cf1cb9c | 37 | 37 |
mbed_official | 6:dd8d1cf1cb9c | 38 | 38 |
mbed_official | 6:dd8d1cf1cb9c | 39 | 39 |
mbed_official | 6:dd8d1cf1cb9c | 40 | 40 |
mbed_official | 6:dd8d1cf1cb9c | 41 | 41 |
mbed_official | 6:dd8d1cf1cb9c | 42 | 42 |
mbed_official | 6:dd8d1cf1cb9c | 43 | 43 |
mbed_official | 6:dd8d1cf1cb9c | 44 | 44 |
mbed_official | 6:dd8d1cf1cb9c | 45 | 45 |
mbed_official | 6:dd8d1cf1cb9c | 46 | 46 |
mbed_official | 6:dd8d1cf1cb9c | 47 | 47 |
mbed_official | 6:dd8d1cf1cb9c | 48 | 48 |
mbed_official | 6:dd8d1cf1cb9c | 49 | 49 |
mbed_official | 6:dd8d1cf1cb9c | 50 | 50 |
mbed_official | 6:dd8d1cf1cb9c | 51 | 51 |
mbed_official | 6:dd8d1cf1cb9c | 52 | 52 |
mbed_official | 0:e8f884c94549 | 53 | #include "mbed.h" |
emilmont | 1:a51d8ed156e6 | 54 | #include "EthernetInterface.h" |
mbed_official | 6:dd8d1cf1cb9c | 55 | |
mbed_official | 6:dd8d1cf1cb9c | 56 | #define ECHO_SERVER_PORT 7 |
mbed_official | 6:dd8d1cf1cb9c | 57 | |
mbed_official | 6:dd8d1cf1cb9c | 58 | int main (void) { |
emilmont | 1:a51d8ed156e6 | 59 | EthernetInterface eth; |
emilmont | 1:a51d8ed156e6 | 60 | eth.init(); //Use DHCP |
emilmont | 1:a51d8ed156e6 | 61 | eth.connect(); |
mbed_official | 6:dd8d1cf1cb9c | 62 | printf("\nServer IP Address is %s\n", eth.getIPAddress()); |
emilmont | 1:a51d8ed156e6 | 63 | |
mbed_official | 6:dd8d1cf1cb9c | 64 | TCPSocketServer server; |
mbed_official | 6:dd8d1cf1cb9c | 65 | server.bind(ECHO_SERVER_PORT); |
mbed_official | 6:dd8d1cf1cb9c | 66 | server.listen(); |
emilmont | 1:a51d8ed156e6 | 67 | |
mbed_official | 6:dd8d1cf1cb9c | 68 | while (true) { |
mbed_official | 6:dd8d1cf1cb9c | 69 | printf("\nWait for new connection...\n"); |
mbed_official | 6:dd8d1cf1cb9c | 70 | TCPSocketConnection client; |
mbed_official | 6:dd8d1cf1cb9c | 71 | server.accept(client); |
mbed_official | 6:dd8d1cf1cb9c | 72 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
mbed_official | 6:dd8d1cf1cb9c | 73 | |
mbed_official | 6:dd8d1cf1cb9c | 74 | printf("Connection from: %s\n", client.get_address()); |
mbed_official | 6:dd8d1cf1cb9c | 75 | char buffer[256]; |
mbed_official | 6:dd8d1cf1cb9c | 76 | while (true) { |
mbed_official | 6:dd8d1cf1cb9c | 77 | int n = client.receive(buffer, sizeof(buffer)); |
mbed_official | 6:dd8d1cf1cb9c | 78 | if (n <= 0) break; |
mbed_official | 6:dd8d1cf1cb9c | 79 | |
mbed_official | 6:dd8d1cf1cb9c | 80 | // print received message to terminal |
mbed_official | 6:dd8d1cf1cb9c | 81 | buffer[n] = '\0'; |
mbed_official | 6:dd8d1cf1cb9c | 82 | printf("Received message from Client :'%s'\n",buffer); |
mbed_official | 6:dd8d1cf1cb9c | 83 | |
mbed_official | 6:dd8d1cf1cb9c | 84 | // reverse the message |
mbed_official | 6:dd8d1cf1cb9c | 85 | char temp; |
mbed_official | 6:dd8d1cf1cb9c | 86 | for(int f = 0, l = n-1; f<l; f++,l--){ |
mbed_official | 6:dd8d1cf1cb9c | 87 | temp = buffer[f]; |
mbed_official | 6:dd8d1cf1cb9c | 88 | buffer[f] = buffer[l]; |
mbed_official | 6:dd8d1cf1cb9c | 89 | buffer[l] = temp; |
mbed_official | 6:dd8d1cf1cb9c | 90 | } |
mbed_official | 6:dd8d1cf1cb9c | 91 | |
mbed_official | 6:dd8d1cf1cb9c | 92 | // print reversed message to terminal |
mbed_official | 6:dd8d1cf1cb9c | 93 | printf("Sending message to Client: '%s'\n",buffer); |
mbed_official | 6:dd8d1cf1cb9c | 94 | |
mbed_official | 6:dd8d1cf1cb9c | 95 | // Echo received message back to client |
mbed_official | 6:dd8d1cf1cb9c | 96 | client.send_all(buffer, n); |
mbed_official | 6:dd8d1cf1cb9c | 97 | if (n <= 0) break; |
mbed_official | 6:dd8d1cf1cb9c | 98 | } |
mbed_official | 6:dd8d1cf1cb9c | 99 | |
mbed_official | 6:dd8d1cf1cb9c | 100 | client.close(); |
mbed_official | 6:dd8d1cf1cb9c | 101 |