TCP Echo Server for INDIA academy
Dependencies: WIZnetInterface mbed
main.cpp@0:964e12c2946e, 2017-12-06 (annotated)
- Committer:
- joon874
- Date:
- Wed Dec 06 07:48:41 2017 +0000
- Revision:
- 0:964e12c2946e
- Child:
- 1:f8dd2f0193fe
TCP Echo Server example for India arcademy
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joon874 | 0:964e12c2946e | 1 | #include "mbed.h" |
joon874 | 0:964e12c2946e | 2 | #include "EthernetInterface.h" |
joon874 | 0:964e12c2946e | 3 | |
joon874 | 0:964e12c2946e | 4 | // Declare Ethernet Class |
joon874 | 0:964e12c2946e | 5 | EthernetInterface eth; |
joon874 | 0:964e12c2946e | 6 | |
joon874 | 0:964e12c2946e | 7 | // Declare Server Class |
joon874 | 0:964e12c2946e | 8 | TCPSocketServer server; |
joon874 | 0:964e12c2946e | 9 | |
joon874 | 0:964e12c2946e | 10 | // Declare Client Class |
joon874 | 0:964e12c2946e | 11 | TCPSocketConnection client; |
joon874 | 0:964e12c2946e | 12 | |
joon874 | 0:964e12c2946e | 13 | //Set Server Network |
joon874 | 0:964e12c2946e | 14 | uint8_t echo_server_mac[] = {0x00, 0x08, 0xDC, 0xFF, 0xFF, 0xD2}; |
joon874 | 0:964e12c2946e | 15 | char echo_server_ip[] = "192.168.0.100"; |
joon874 | 0:964e12c2946e | 16 | char echo_server_subnet[] = "255.255.255.0"; |
joon874 | 0:964e12c2946e | 17 | char echo_server_gateway[] = "192.168.0.1"; |
joon874 | 0:964e12c2946e | 18 | int echo_server_port = 30000; |
joon874 | 0:964e12c2946e | 19 | |
joon874 | 0:964e12c2946e | 20 | int main (void) |
joon874 | 0:964e12c2946e | 21 | { |
joon874 | 0:964e12c2946e | 22 | printf("Wait a second...\r\n"); |
joon874 | 0:964e12c2946e | 23 | eth.init(echo_server_mac, echo_server_ip, echo_server_subnet, echo_server_gateway); |
joon874 | 0:964e12c2946e | 24 | eth.connect(); |
joon874 | 0:964e12c2946e | 25 | |
joon874 | 0:964e12c2946e | 26 | printf("Server MAC Address is %s\r\n", eth.getMACAddress()); |
joon874 | 0:964e12c2946e | 27 | printf("Server IP Address is %s\r\n", eth.getIPAddress()); |
joon874 | 0:964e12c2946e | 28 | printf("Server SUBNET Address is %s\r\n", eth.getNetworkMask()); |
joon874 | 0:964e12c2946e | 29 | printf("Server GATEWAY Address is %s\r\n", eth.getGateway()); |
joon874 | 0:964e12c2946e | 30 | |
joon874 | 0:964e12c2946e | 31 | server.bind(echo_server_port); |
joon874 | 0:964e12c2946e | 32 | server.listen(); |
joon874 | 0:964e12c2946e | 33 | |
joon874 | 0:964e12c2946e | 34 | while (true) |
joon874 | 0:964e12c2946e | 35 | { |
joon874 | 0:964e12c2946e | 36 | printf("Wait for new connection...\r\n"); |
joon874 | 0:964e12c2946e | 37 | server.accept(client); |
joon874 | 0:964e12c2946e | 38 | client.set_blocking(false, 15000); // Timeout after (1.5)s |
joon874 | 0:964e12c2946e | 39 | |
joon874 | 0:964e12c2946e | 40 | printf("Connection from: %s\r\n", client.get_address()); |
joon874 | 0:964e12c2946e | 41 | char buffer[256]; |
joon874 | 0:964e12c2946e | 42 | while (true) { |
joon874 | 0:964e12c2946e | 43 | int n = client.receive(buffer, sizeof(buffer)); |
joon874 | 0:964e12c2946e | 44 | if (n <= 0) break; |
joon874 | 0:964e12c2946e | 45 | |
joon874 | 0:964e12c2946e | 46 | // print received message to terminal |
joon874 | 0:964e12c2946e | 47 | buffer[n] = '\0'; |
joon874 | 0:964e12c2946e | 48 | printf("Received message from Client :'%s'\r\n",buffer); |
joon874 | 0:964e12c2946e | 49 | |
joon874 | 0:964e12c2946e | 50 | // print sending message to terminal |
joon874 | 0:964e12c2946e | 51 | printf("Sending message to Client: '%s'\r\n",buffer); |
joon874 | 0:964e12c2946e | 52 | |
joon874 | 0:964e12c2946e | 53 | // Echo received message back to client |
joon874 | 0:964e12c2946e | 54 | client.send_all(buffer, n); |
joon874 | 0:964e12c2946e | 55 | if (n <= 0) break; |
joon874 | 0:964e12c2946e | 56 | } |
joon874 | 0:964e12c2946e | 57 | |
joon874 | 0:964e12c2946e | 58 | client.close(); |
joon874 | 0:964e12c2946e | 59 | } |
joon874 | 0:964e12c2946e | 60 | |
joon874 | 0:964e12c2946e | 61 | } |
joon874 | 0:964e12c2946e | 62 |