HofSpannung / Mbed 2 deprecated UDP_Server_WORKING

Dependencies:   mbed

Fork of UDP_V1_0 by HofSpannung

main.cpp

Committer:
jdeitert
Date:
2018-06-05
Revision:
0:acad836c53e8
Child:
1:be0485e31b72

File content as of revision 0:acad836c53e8:

//code working

#if !FEATURE_LWIP
    #error [NOT_SUPPORTED] LWIP not supported for this target
#endif

#include "mbed.h"
#include "EthernetInterface.h"
//#include "TCPServer.h"
//#include "TCPSocket.h"

#define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
#define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
#define HTTP_MESSAGE_BODY ""                                     \
"<html>" "\r\n"                                                  \
"  <body style=\"display:flex;text-align:center\">" "\r\n"       \
"    <div style=\"margin:auto\">" "\r\n"                         \
"      <h1>Hello World</h1>" "\r\n"                              \
"      <p>It works !</p>" "\r\n"                                 \
"    </div>" "\r\n"                                              \
"  </body>" "\r\n"                                               \
"</html>"

#define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n"   \
                      HTTP_HEADER_FIELDS "\r\n" \
                      "\r\n"                    \
                      HTTP_MESSAGE_BODY "\r\n"

int main()
{
    printf("Basic HTTP server example\n");
    
    EthernetInterface eth;
    eth.connect();
    
    printf("The target IP address is '%s'\r\n", eth.get_ip_address());
    
    UDPSocket socket;
    printf("Hello Succ\r\n");
    
    if(socket.open(&eth)!=0)
    {
        printf("Opening failed\r\n");
        while(1);
    }
    
    if(socket.bind(2223)!=0)
    {
        printf("Binding failed\r\n");
        while(1);
    }

    char buf[256];
    SocketAddress sender;
    printf("Before while...\r\n");
    while (true) {
        int recBytes = socket.recvfrom(&sender,buf,256);
        printf("Received packet. Size: %d\r\n",recBytes);
        if(recBytes)
        {
            socket.sendto(sender,buf,recBytes);
            printf("Sent echo\r\n");
        }
        
    }
}