For lecture UDP

Dependencies:   WIZnetInterface WIZwiki-W7500-lecture mbed

Fork of WIZwiki-W7500-lecture by Bohyun Bang

main.cpp

Committer:
bangbh
Date:
2016-02-01
Revision:
3:f549134aa6c4
Parent:
2:ba228e5b659c

File content as of revision 3:f549134aa6c4:

#include "mbed.h"
#include "EthernetInterface.h"

int main() {
    
    EthernetInterface eth;
    
    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
    eth.init(mac_addr, "192.168.0.180", "255.255.255.0", "192.168.0.1"); // USE Static IP
    //eth.init(mac_addr); //Use DHCP
    eth.connect();
     
    printf("IP Address is %s\r\n", eth.getIPAddress());

    UDPSocket udp_sock;
    udp_sock.init();
    udp_sock.bind(60001);
    Endpoint udp_server;
    udp_server.set_address("192.168.0.244",4023);
    char message[] = "Hello World!!\r\n";
    udp_sock.sendTo(udp_server,message,sizeof(message)-1);
    char buffer[400];
    int ret;
    while (true) {
        ret = udp_sock.receiveFrom(udp_server, buffer, sizeof(buffer)-1);
        if (ret > 0){
            buffer[ret] = '\0';
            printf("Received %d chars from server: %s\n", ret, buffer);
        }
    }
    eth.disconnect();
    
    printf("Disconnected!"); 
}