Published

Dependencies:   DHT MQTTforLecture WIZnetInterface mbed-src

Fork of w7500-paho-mqtt by Bohyun Bang

Committer:
justinkim
Date:
Tue Jun 23 00:36:34 2015 +0000
Revision:
8:f837e0d255e8
Parent:
7:a5ead1402704
Child:
9:21574e058141
TCPEchoServer example for WIZwiki-W7500

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 8:f837e0d255e8 1 #include "mbed.h"
justinkim 8:f837e0d255e8 2 #include "EthernetInterface.h"
justinkim 8:f837e0d255e8 3
justinkim 8:f837e0d255e8 4 #define ECHO_SERVER_PORT 7
justinkim 8:f837e0d255e8 5
justinkim 8:f837e0d255e8 6 int main (void)
justinkim 8:f837e0d255e8 7 {
justinkim 8:f837e0d255e8 8 printf("Wait a second...\r\n");
justinkim 8:f837e0d255e8 9 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
justinkim 8:f837e0d255e8 10 EthernetInterface eth;
justinkim 8:f837e0d255e8 11 eth.init(mac_addr); //Use DHCP
justinkim 8:f837e0d255e8 12 eth.connect();
justinkim 8:f837e0d255e8 13 printf("Server IP Address is %s\r\n", eth.getIPAddress());
justinkim 8:f837e0d255e8 14
justinkim 8:f837e0d255e8 15 TCPSocketServer server;
justinkim 8:f837e0d255e8 16 server.bind(ECHO_SERVER_PORT);
justinkim 8:f837e0d255e8 17 server.listen();
justinkim 8:f837e0d255e8 18
justinkim 8:f837e0d255e8 19 while (true)
justinkim 8:f837e0d255e8 20 {
justinkim 8:f837e0d255e8 21 printf("Wait for new connection...\r\n");
justinkim 8:f837e0d255e8 22 TCPSocketConnection client;
justinkim 8:f837e0d255e8 23 server.accept(client);
justinkim 8:f837e0d255e8 24 client.set_blocking(false, 15000); // Timeout after (1.5)s
justinkim 8:f837e0d255e8 25
justinkim 8:f837e0d255e8 26 printf("Connection from: %s\r\n", client.get_address());
justinkim 8:f837e0d255e8 27 char buffer[256];
justinkim 8:f837e0d255e8 28 while (true) {
justinkim 8:f837e0d255e8 29 int n = client.receive(buffer, sizeof(buffer));
justinkim 8:f837e0d255e8 30 if (n <= 0) break;
justinkim 8:f837e0d255e8 31
justinkim 8:f837e0d255e8 32 // print received message to terminal
justinkim 8:f837e0d255e8 33 buffer[n] = '\0';
justinkim 8:f837e0d255e8 34 printf("Received message from Client :'%s'\r\n",buffer);
justinkim 8:f837e0d255e8 35
justinkim 8:f837e0d255e8 36 // reverse the message
justinkim 8:f837e0d255e8 37 char temp;
justinkim 8:f837e0d255e8 38 for(int f = 0, l = n-1; f<l; f++,l--){
justinkim 8:f837e0d255e8 39 temp = buffer[f];
justinkim 8:f837e0d255e8 40 buffer[f] = buffer[l];
justinkim 8:f837e0d255e8 41 buffer[l] = temp;
justinkim 8:f837e0d255e8 42 }
justinkim 8:f837e0d255e8 43
justinkim 8:f837e0d255e8 44 // print reversed message to terminal
justinkim 8:f837e0d255e8 45 printf("Sending message to Client: '%s'\r\n",buffer);
justinkim 8:f837e0d255e8 46
justinkim 8:f837e0d255e8 47 // Echo received message back to client
justinkim 8:f837e0d255e8 48 client.send_all(buffer, n);
justinkim 8:f837e0d255e8 49 if (n <= 0) break;
justinkim 8:f837e0d255e8 50 }
justinkim 8:f837e0d255e8 51
justinkim 8:f837e0d255e8 52 client.close();
justinkim 8:f837e0d255e8 53 }
justinkim 8:f837e0d255e8 54 }
justinkim 8:f837e0d255e8 55