TCP Echo Server for INDIA academy

Dependencies:   WIZnetInterface mbed

hercules download

Committer:
joon874
Date:
Wed Dec 06 23:51:34 2017 +0000
Revision:
1:f8dd2f0193fe
Parent:
0:964e12c2946e
change close method

Who changed what in which revision?

UserRevisionLine numberNew 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 1:f8dd2f0193fe 20 //Close message
joon874 1:f8dd2f0193fe 21 char close_message[256] = "close";
joon874 1:f8dd2f0193fe 22
joon874 0:964e12c2946e 23 int main (void)
joon874 0:964e12c2946e 24 {
joon874 0:964e12c2946e 25 printf("Wait a second...\r\n");
joon874 0:964e12c2946e 26 eth.init(echo_server_mac, echo_server_ip, echo_server_subnet, echo_server_gateway);
joon874 0:964e12c2946e 27 eth.connect();
joon874 0:964e12c2946e 28
joon874 0:964e12c2946e 29 printf("Server MAC Address is %s\r\n", eth.getMACAddress());
joon874 0:964e12c2946e 30 printf("Server IP Address is %s\r\n", eth.getIPAddress());
joon874 0:964e12c2946e 31 printf("Server SUBNET Address is %s\r\n", eth.getNetworkMask());
joon874 0:964e12c2946e 32 printf("Server GATEWAY Address is %s\r\n", eth.getGateway());
joon874 0:964e12c2946e 33
joon874 0:964e12c2946e 34 server.bind(echo_server_port);
joon874 0:964e12c2946e 35 server.listen();
joon874 0:964e12c2946e 36
joon874 0:964e12c2946e 37 while (true)
joon874 0:964e12c2946e 38 {
joon874 0:964e12c2946e 39 printf("Wait for new connection...\r\n");
joon874 0:964e12c2946e 40 server.accept(client);
joon874 0:964e12c2946e 41 client.set_blocking(false, 15000); // Timeout after (1.5)s
joon874 0:964e12c2946e 42
joon874 0:964e12c2946e 43 printf("Connection from: %s\r\n", client.get_address());
joon874 0:964e12c2946e 44 char buffer[256];
joon874 0:964e12c2946e 45 while (true) {
joon874 0:964e12c2946e 46 int n = client.receive(buffer, sizeof(buffer));
joon874 1:f8dd2f0193fe 47 buffer[n] = '\0';
joon874 0:964e12c2946e 48
joon874 1:f8dd2f0193fe 49 if (strcmp(buffer,close_message)==0) {
joon874 1:f8dd2f0193fe 50 printf("recv close message \r\n");
joon874 1:f8dd2f0193fe 51 break;
joon874 1:f8dd2f0193fe 52 }
joon874 1:f8dd2f0193fe 53 else {
joon874 1:f8dd2f0193fe 54 if (n > 0) {
joon874 1:f8dd2f0193fe 55 // print received message to terminal
joon874 1:f8dd2f0193fe 56 printf("Received message from Client :'%s'\r\n",buffer);
joon874 0:964e12c2946e 57
joon874 1:f8dd2f0193fe 58 // print sending message to terminal
joon874 1:f8dd2f0193fe 59 printf("Sending message to Client: '%s'\r\n",buffer);
joon874 0:964e12c2946e 60
joon874 1:f8dd2f0193fe 61 // Echo received message back to client
joon874 1:f8dd2f0193fe 62 client.send_all(buffer, n);
joon874 1:f8dd2f0193fe 63 }
joon874 1:f8dd2f0193fe 64 }
joon874 0:964e12c2946e 65 }
joon874 0:964e12c2946e 66
joon874 0:964e12c2946e 67 client.close();
joon874 0:964e12c2946e 68 }
joon874 0:964e12c2946e 69
joon874 0:964e12c2946e 70 }
joon874 0:964e12c2946e 71