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: WIZnetInterface mbed
Fork of TCPEchoServer-WIZwiki-W7500 by
main.cpp@12:30006dcdb92d, 2016-04-06 (annotated)
- Committer:
- ipv1
- Date:
- Wed Apr 06 08:11:01 2016 +0000
- Revision:
- 12:30006dcdb92d
- Parent:
- 11:0da8667a9201
Dummy but working text as well as commands
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| justinkim | 8:f837e0d255e8 | 1 | #include "mbed.h" |
| justinkim | 8:f837e0d255e8 | 2 | #include "EthernetInterface.h" |
| hjjeon | 9:a63ff95c354b | 3 | |
| hjjeon | 11:0da8667a9201 | 4 | #define ECHO_SERVER_PORT 7 |
| hjjeon | 9:a63ff95c354b | 5 | |
| ipv1 | 12:30006dcdb92d | 6 | |
| hjjeon | 11:0da8667a9201 | 7 | int main (void) |
| justinkim | 8:f837e0d255e8 | 8 | { |
| ipv1 | 12:30006dcdb92d | 9 | DigitalOut myled(LED1); |
| justinkim | 8:f837e0d255e8 | 10 | printf("Wait a second...\r\n"); |
| hjjeon | 11:0da8667a9201 | 11 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; |
| ipv1 | 12:30006dcdb92d | 12 | static const char* mbedIp = "192.168.64.232"; //IP |
| ipv1 | 12:30006dcdb92d | 13 | static const char* mbedMask = "255.255.255.0"; // Mask |
| ipv1 | 12:30006dcdb92d | 14 | static const char* mbedGateway = "192.168.64.1"; //Gateway |
| ipv1 | 12:30006dcdb92d | 15 | |
| hjjeon | 11:0da8667a9201 | 16 | EthernetInterface eth; |
| ipv1 | 12:30006dcdb92d | 17 | eth.init(mac_addr, mbedIp, mbedMask, mbedGateway); //Use DHCP |
| justinkim | 8:f837e0d255e8 | 18 | eth.connect(); |
| justinkim | 8:f837e0d255e8 | 19 | printf("Server IP Address is %s\r\n", eth.getIPAddress()); |
| hjjeon | 11:0da8667a9201 | 20 | |
| hjjeon | 11:0da8667a9201 | 21 | TCPSocketServer server; |
| hjjeon | 11:0da8667a9201 | 22 | server.bind(ECHO_SERVER_PORT); |
| hjjeon | 11:0da8667a9201 | 23 | server.listen(); |
| hjjeon | 11:0da8667a9201 | 24 | |
| hjjeon | 11:0da8667a9201 | 25 | while (true) |
| hjjeon | 11:0da8667a9201 | 26 | { |
| hjjeon | 11:0da8667a9201 | 27 | printf("Wait for new connection...\r\n"); |
| hjjeon | 11:0da8667a9201 | 28 | TCPSocketConnection client; |
| hjjeon | 11:0da8667a9201 | 29 | server.accept(client); |
| hjjeon | 11:0da8667a9201 | 30 | client.set_blocking(false, 15000); // Timeout after (1.5)s |
| hjjeon | 11:0da8667a9201 | 31 | |
| hjjeon | 11:0da8667a9201 | 32 | printf("Connection from: %s\r\n", client.get_address()); |
| hjjeon | 11:0da8667a9201 | 33 | char buffer[256]; |
| hjjeon | 11:0da8667a9201 | 34 | while (true) { |
| hjjeon | 11:0da8667a9201 | 35 | int n = client.receive(buffer, sizeof(buffer)); |
| hjjeon | 11:0da8667a9201 | 36 | if (n <= 0) break; |
| hjjeon | 11:0da8667a9201 | 37 | |
| ipv1 | 12:30006dcdb92d | 38 | if(buffer[0]=='Y'){ |
| ipv1 | 12:30006dcdb92d | 39 | myled=1; |
| ipv1 | 12:30006dcdb92d | 40 | }else{ |
| ipv1 | 12:30006dcdb92d | 41 | myled=0; |
| ipv1 | 12:30006dcdb92d | 42 | } |
| hjjeon | 11:0da8667a9201 | 43 | // print received message to terminal |
| ipv1 | 12:30006dcdb92d | 44 | //buffer[n] = '\0'; |
| ipv1 | 12:30006dcdb92d | 45 | //printf("Received message from Client :'%s'\r\n",buffer); |
| hjjeon | 11:0da8667a9201 | 46 | |
| hjjeon | 11:0da8667a9201 | 47 | // reverse the message |
| ipv1 | 12:30006dcdb92d | 48 | //char temp; |
| ipv1 | 12:30006dcdb92d | 49 | // for(int f = 0, l = n-1; f<l; f++,l--){ |
| ipv1 | 12:30006dcdb92d | 50 | // temp = buffer[f]; |
| ipv1 | 12:30006dcdb92d | 51 | // buffer[f] = buffer[l]; |
| ipv1 | 12:30006dcdb92d | 52 | // buffer[l] = temp; |
| ipv1 | 12:30006dcdb92d | 53 | // } |
| ipv1 | 12:30006dcdb92d | 54 | sprintf(buffer, "Hi Alisha.How are u?"); |
| hjjeon | 11:0da8667a9201 | 55 | // print reversed message to terminal |
| hjjeon | 11:0da8667a9201 | 56 | printf("Sending message to Client: '%s'\r\n",buffer); |
| hjjeon | 11:0da8667a9201 | 57 | |
| hjjeon | 11:0da8667a9201 | 58 | // Echo received message back to client |
| hjjeon | 11:0da8667a9201 | 59 | client.send_all(buffer, n); |
| hjjeon | 11:0da8667a9201 | 60 | if (n <= 0) break; |
| hjjeon | 11:0da8667a9201 | 61 | } |
| hjjeon | 11:0da8667a9201 | 62 | |
| hjjeon | 11:0da8667a9201 | 63 | client.close(); |
| hjjeon | 9:a63ff95c354b | 64 | } |
| justinkim | 8:f837e0d255e8 | 65 | |
| justinkim | 8:f837e0d255e8 | 66 | } |
| justinkim | 8:f837e0d255e8 | 67 |
