[En-Nut-Discussion] UDP BroadCast

Avex avexwu at yahoo.com.tw
Fri Sep 20 05:51:37 CEST 2002


Hi all,

I tested UDP 'NutUdpSendTo' function and fail before (OS 2.5.2).
So I wrote a simple test program to test it. I found, broadcast 
function is not workable, peer to peer is ok. What's wrong with 
it ? Hope someone can help me.

see codes below.


#include <string.h>
#include <dev/nicrtl.h>
#include <sys/heap.h>
#include <sys/timer.h>
#include <dev/uartavr.h>
#include <sys/thread.h>
#include <sys/print.h>
#include <sys/confnet.h>
#include <netinet/sostream.h>
#include <arpa/inet.h>

THREAD(NutMain, arg)
{
    UDPSOCKET* sock;
    NUTDEVICE *uart;
    u_long baud = 9600;
    char *data = "test";
    char *p;
    // for UDP
     unsigned char buff[512];
     u_long address;
     UDPSOCKET *sock_udp;
     int nRead;
     u_short cli_port;

    // Register Realtek controller at address 8300 hex
    // and interrupt 5 and configure lan interface.

    NutRegisterDevice(&devUart0, 0, 0);
    NutNetAutoConfig("eth0");

    uart = NutDeviceOpen("uart0");
    NutDeviceIOCtl(uart, UART_SETSPEED, &baud);
    NutRegisterDevice(&devEth0, 0x8300, 5);
    NutNetAutoConfig("eth0");

    NutPrintFormat(uart, "program started!");
    NutPrintFlush(uart);

    // Create a socket.
    sock_udp = NutUdpCreateSocket(1234);

    while(1)
    {

        if((nRead = NutUdpReceiveFrom(sock_udp, &address, &cli_port, buff, sizeof(buff), 600000)) > 0)
        {
            buff[nRead] = 0;
            NutPrintFormat(uart, "Data received :%s\r\n", buff);
            p = (char*)&address;
            NutPrintFormat(uart, "address :%d,%d,%d,%d", p[3],p[2],p[1],p[0]);
            NutPrintFormat(uart, "%s\r\n",inet_ntoa(address));
            NutPrintFlush(uart);

            NutUdpSendTo(sock_udp, 0xffffffff, 1234, data, 4);

            // the line below is not workable. as echo
            NutUdpSendTo(sock_udp, inet_addr("255.255.255.255"), 1234, buff, nRead);

            // the line below is workable. send "test" string 
            NutUdpSendTo(sock_udp, address, 1234, buff, nRead);
        }

    }

    NutUdpDestroySocket(sock_udp);

}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.egnite.de/pipermail/en-nut-discussion/attachments/20020920/c065ed8e/attachment.html>


More information about the En-Nut-Discussion mailing list