ex_udp.c

The following example will create a UDP socket that listens to port 54123. If anything is received on this port, it will check the first byte of the received data. If it is 1, it will set the system LED (Port B6), if 0, it will clear the system LED.

00001 
00008 #include "net\udp.h"
00009 
00010 //Create a UDP socket for receiving and sending data
00011 static UDP_SOCKET udpSocketUser = INVALID_UDP_SOCKET;
00012 
00013 
00014 void udpExample(void)
00015 {
00016     BYTE c;
00017     NODE_INFO udpServerNode;
00018 
00019     //Initialize remote IP and MAC address of udpServerNode with 0, seeing that we
00020     //don't know them for the node that will send us an UDP message. The first time
00021     //a message is received addressed to this port, the remote IP and MAC addresses
00022     //are automatically updated with the addresses of the remote node.
00023     memclr(&udpServerNode, sizeof(udpServerNode));
00024 
00025     //Configure for local port 54123 and remote port INVALID_UDP_PORT. This opens
00026     //the socket to listen on the given port.
00027     udpSocketUser = UDPOpen(54123, &udpServerNode, INVALID_UDP_PORT);
00028     
00029     //An error occurred during the UDPOpen() function
00030     if (udpSocketUser == INVALID_UDP_SOCKET) {
00031         //Add user code here to take action if required!
00032     }
00033 
00034     //Infinite loop. Check if anything is received on UDP port
00035     while(1)
00036     {
00037         //Is there any data waiting for us on the UDP socket?
00038         //Because of the design of the Modtronix TCP/IP stack we have to consume
00039         //all data sent to us as soon as we detect it.
00040         if (UDPIsGetReady(udpSocketUser)) {
00041 
00042             //We are only interrested in the first byte of the message.
00043             UDPGet(&c);
00044             
00045             if (c == '0') LATB6 = 0;        //Switch system LED off
00046             else if (c == '1') LATB6 = 1;   //Switch system LED on
00047 
00048             //Discard the socket buffer.
00049             UDPDiscard();
00050         }
00051 
00052         //This task performs normal stack task including checking for incoming
00053         //packet, type of packet and calling appropriate stack entity to
00054         //process it.
00055         StackTask();
00056     }
00057 }

Generated on Mon Oct 9 13:22:06 2006 for SBC65EC Web Server by  doxygen 1.4.7