WIZ Proto Makers 3th Lab 2

Dependencies:   WIZnetInterface mbed

Fork of WIZ_Proto_Makers_3th_Lab1 by -deleted-

Committer:
Ricky_Kwon
Date:
Wed Feb 03 03:16:46 2016 +0000
Revision:
2:6a79ff33db7f
Parent:
1:a4e5ec9f59cc
WIZ Proto Makers 3th Lab 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ricky_Kwon 0:722e7ff3a065 1 #include "mbed.h"
Ricky_Kwon 0:722e7ff3a065 2 #include "EthernetInterface.h"
Ricky_Kwon 0:722e7ff3a065 3
Ricky_Kwon 0:722e7ff3a065 4 #define WEB_SERVER_PORT 80
Ricky_Kwon 0:722e7ff3a065 5
Ricky_Kwon 2:6a79ff33db7f 6 char send_dat[]="HTTP/1.1 200 OK\r\n"\
Ricky_Kwon 2:6a79ff33db7f 7 "Content-Type: text/html\r\n"\
Ricky_Kwon 2:6a79ff33db7f 8 "Connection: close\r\n\r\n"\
Ricky_Kwon 2:6a79ff33db7f 9 "<html>\r\n"\
Ricky_Kwon 2:6a79ff33db7f 10 "<body>"\
Ricky_Kwon 2:6a79ff33db7f 11 "Hello world"\
Ricky_Kwon 2:6a79ff33db7f 12 "</body>\r\n"\
Ricky_Kwon 2:6a79ff33db7f 13 "</html>\r\n";
Ricky_Kwon 2:6a79ff33db7f 14
Ricky_Kwon 0:722e7ff3a065 15 int main (void)
Ricky_Kwon 0:722e7ff3a065 16 {
Ricky_Kwon 0:722e7ff3a065 17 printf("Wait a second...\r\n");
Ricky_Kwon 0:722e7ff3a065 18 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
Ricky_Kwon 0:722e7ff3a065 19 const char ip_addr[] = "192.168.0.222";
Ricky_Kwon 0:722e7ff3a065 20 const char mask_addr[] = "255.255.255.0";
Ricky_Kwon 0:722e7ff3a065 21 const char gateway_addr[] = "192.168.0.1";
Ricky_Kwon 0:722e7ff3a065 22
Ricky_Kwon 0:722e7ff3a065 23 EthernetInterface eth;
Ricky_Kwon 0:722e7ff3a065 24 //eth.init(mac_addr); //Use DHCP
Ricky_Kwon 0:722e7ff3a065 25 eth.init(mac_addr, ip_addr, mask_addr, gateway_addr); //Use Static
Ricky_Kwon 0:722e7ff3a065 26 eth.connect();
Ricky_Kwon 0:722e7ff3a065 27 printf("Server IP Address is %s\r\n", eth.getIPAddress());
Ricky_Kwon 0:722e7ff3a065 28
Ricky_Kwon 0:722e7ff3a065 29 TCPSocketServer server;
Ricky_Kwon 0:722e7ff3a065 30 server.bind(WEB_SERVER_PORT);
Ricky_Kwon 0:722e7ff3a065 31 server.listen();
Ricky_Kwon 0:722e7ff3a065 32
Ricky_Kwon 0:722e7ff3a065 33 while (true)
Ricky_Kwon 0:722e7ff3a065 34 {
Ricky_Kwon 0:722e7ff3a065 35 printf("Wait for new connection...\r\n");
Ricky_Kwon 0:722e7ff3a065 36 TCPSocketConnection client;
Ricky_Kwon 0:722e7ff3a065 37 server.accept(client);
Ricky_Kwon 0:722e7ff3a065 38 client.set_blocking(false, 15000); // Timeout after (1.5)s
Ricky_Kwon 0:722e7ff3a065 39
Ricky_Kwon 0:722e7ff3a065 40 printf("Connection from: %s\r\n", client.get_address());
Ricky_Kwon 0:722e7ff3a065 41 char buffer[256];
Ricky_Kwon 0:722e7ff3a065 42
Ricky_Kwon 0:722e7ff3a065 43 while (true) {
Ricky_Kwon 0:722e7ff3a065 44
Ricky_Kwon 0:722e7ff3a065 45 wait(0.5);
Ricky_Kwon 0:722e7ff3a065 46 int n = client.receive_all(buffer, sizeof(buffer));
Ricky_Kwon 0:722e7ff3a065 47 if (n <= 0) break;
Ricky_Kwon 0:722e7ff3a065 48 //buffer[n] = '\0';
Ricky_Kwon 0:722e7ff3a065 49
Ricky_Kwon 0:722e7ff3a065 50 printf("rev_dat : %s\r\n", buffer);
Ricky_Kwon 2:6a79ff33db7f 51
Ricky_Kwon 2:6a79ff33db7f 52 if((buffer[0]=='G')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]==' '))// GET_Request
Ricky_Kwon 2:6a79ff33db7f 53 {
Ricky_Kwon 2:6a79ff33db7f 54 client.send(send_dat, sizeof(send_dat));
Ricky_Kwon 2:6a79ff33db7f 55 printf("send_dat : %s\r\n", send_dat);
Ricky_Kwon 2:6a79ff33db7f 56 }
Ricky_Kwon 1:a4e5ec9f59cc 57
Ricky_Kwon 0:722e7ff3a065 58 }
Ricky_Kwon 0:722e7ff3a065 59
Ricky_Kwon 0:722e7ff3a065 60 client.close();
Ricky_Kwon 0:722e7ff3a065 61 }
Ricky_Kwon 0:722e7ff3a065 62
Ricky_Kwon 0:722e7ff3a065 63 }
Ricky_Kwon 0:722e7ff3a065 64