A simple HTTP server echoing received requests. Ethernet connection is over an ENC28J60 board. Usage: Type the server's IP address into you web browser and hit <ENTER>.

Dependencies:   UIPEthernet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * In this example the HTTP request (text) received from a browser is echoed (sent back) to the browser.
00003  * Ethernet connection is via an ENC28J60 Ethernet board driven by the UIPEthernet library
00004  */
00005 #include "mbed.h"
00006 #include "UipEthernet.h"
00007 
00008 #define IP      "192.168.1.35"
00009 #define GATEWAY "192.168.1.1"
00010 #define NETMASK "255.255.255.0"
00011 #define PORT    80
00012 
00013 const uint8_t   MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
00014 UipEthernet     net(MAC, D11, D12, D13, D10);   // mac, mosi, miso, sck, cs
00015 //UipEthernet     net(MAC, PA_7, PA_6, PA_5, PB_6);   // mac, mosi, miso, sck, cs
00016 TcpServer       server;                         // Ethernet server
00017 TcpClient*      client;
00018 char            receiveBuf[1024];
00019 char            echoHeader[256];
00020 /**
00021  * @brief
00022  * @note
00023  * @param
00024  * @retval
00025  */
00026 int main(void)
00027 {
00028     printf("Starting ...\r\n");
00029 
00030     //net.set_network(IP, NETMASK, GATEWAY);  // include this to use static IP address
00031     net.connect();
00032 
00033     // Show the network address
00034     const char*     ip = net.get_ip_address();
00035     const char*     netmask = net.get_netmask();
00036     const char*     gateway = net.get_gateway();
00037 
00038     printf("IP address: %s\r\n", ip ? ip : "None");
00039     printf("Netmask: %s\r\n", netmask ? netmask : "None");
00040     printf("Gateway: %s\r\n\r\n", gateway ? gateway : "None");
00041     printf("------------------------------------------------------\r\n");
00042     printf("Usage: Type %s into your web browser and hit ENTER\r\n", net.get_ip_address());
00043     printf("------------------------------------------------------\r\n");
00044 
00045     /* Open the server on ethernet stack */
00046     server.open(&net);
00047 
00048     /* Bind the HTTP port (TCP 80) to the server */
00049     server.bind(PORT);
00050 
00051     /* Can handle 5 simultaneous connections */
00052     server.listen(5);
00053 
00054     while (1) {
00055         client = server.accept();
00056 
00057         if (client) {
00058             size_t receiveLen = client->recv((uint8_t*)receiveBuf, client->available());
00059             if (receiveBuf[0] == 'G' && receiveBuf[1] == 'E' && receiveBuf[2] == 'T') {
00060                 printf("\r\n-------------------------------------------------------\r\n");
00061                 printf("GET request received from a client with IP address %s\n\r", client->getpeername());
00062                 printf("%s\r\n", receiveBuf);
00063                 sprintf
00064                 (
00065                     echoHeader,
00066                     "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text\r\nConnection: About to close\r\n\r\n",
00067                     receiveLen
00068                 );
00069                 client->send((uint8_t*)echoHeader, strlen(echoHeader));
00070                 client->send((uint8_t*)receiveBuf, receiveLen);
00071                 printf("Echo done.\r\n");
00072             }
00073             client->close();
00074         }
00075     }
00076 }