ex_udp_echo.c

This example program shows how to send and receive UDP datagrams. This program acts as the server, and listens for any data entering on UDP port 54123. If a UDP message is received, we read it and return data to the same socket (IP and port) that just sent us data. It implements a circular buffer for storing the received data.

#include "net\udp.h"

//The following code is for the Microchip MPLAB C18 compiler. For the
//Hi-Tech compiler, the #pragma's are not required
#pragma udata BUFFER1_256BYTES
BYTE udpRxBuf[256];
#pragma udata   //Return to default section

//Get and Put pointer for udpRxBuf circular buffer. If Get pointer = Put
//pointer, the buffer is empty.
BYTE udpRxBufGet, udpRxBufPut;

//Create a UDP socket for receiving and sending data
static UDP_SOCKET udpSocketUser = INVALID_UDP_SOCKET;


void main(void)
{
    BYTE c;
    NODE_INFO udpServerNode;

    //Set UDP Receive buffer to be empty
    udpRxBufGet = udpRxBufPut = 0;

    //Initialize remote IP and MAC address of udpServerNode with 0, seeing that
    //we don't know them for the node that will send us an UDP message. The first
    //time a message is received addressed to this port, the remote IP and MAC
    //addresses are automatically updated with the addresses of the remote node.
    memclr(&udpServerNode, sizeof(udpServerNode));

    //Configure for local port 54123 and remote port INVALID_UDP_PORT. This
    //opens the socket to listed on the given port.
    udpSocketUser = UDPOpen(54123, &udpServerNode, INVALID_UDP_PORT);
    
    //An error occurred during the UDPOpen() function
    if (udpSocketUser == INVALID_UDP_SOCKET) {
        //Take any additional action that is required when an error occurs
    }

    //Infinite loop. Check if anything is received on UDP port
    while(1)
    {
        //Is there any data waiting for us on the UDP socket? Because of the
        //design of the Modtronix TCP/IP stack we have to consume all data
        //sent to us as soon as we detect it. Store all data to a buffer
        //as soon as it is detected
        if (UDPIsGetReady(udpSocketUser)) {

            //Read all data from socket, and save it to UDP Receive buffer
            //(udpRxBuf)
            while(UDPGet(&c)) {
                //Store byte read from UDP socket to UDP buffer
                udpRxBuf[udpRxBufPut++] = c;
            
                //If Put pointer caught up to Get pointer, our buffer is
                //full and we have lost data! Increment Get pointer.
                if (udpRxBufPut == udpRxBufGet) {
                    udpRxBufGet++;
                }
            }

            //Discard the socket buffer.
            UDPDiscard();
        }

        //Does the UDP receive buffer contain any data? If so, we sent this data back
        //to the same socket that just send us data. 
        if(udpRxBufPut != udpRxBufGet) {
            //Checks if there is a transmit buffer ready for accepting data, and that the
            //given socket is valid (not equal to INVALID_UDP_SOCKET for example)
            if (UDPIsPutReady(udpSocketUser)) {
            
                //Transmit entire buffer
                while (udpRxBufGet != udpRxBufPut) {
                    UDPPut(udpRxBuf[udpRxBufGet++]);
                }

                // Now transmit it.
                UDPFlush();
            }
        }

        //This task performs normal stack task including checking for incoming packet,
        //type of packet and calling appropriate stack entity to process it.
        StackTask();
    }
}

Generated on Wed Feb 3 12:45:33 2010 for SBC65EC Web Server by  doxygen 1.5.8