ex_udp_echo2.c

This example program shows how to send and receive UDP datagrams with the UDPGetArray() and UDPPutArray() functions. This program acts as the server, and listens for any data entering on UDP port 54123. If a UDP message is received, is is read and sent back to port 54124 of the node that just send us the message.

00001 
00010 #include "net\udp.h"
00011 
00012 //The following code is for the Microchip MPLAB C18 compiler. For the
00013 //Hi-Tech compiler, the #pragma's are not required
00014 #pragma udata BUFFER1_256BYTES
00015 BYTE udpRxBuf[256];
00016 #pragma udata   //Return to default section
00017 
00018 //Remember number of bytes received from the UDP socket
00019 BYTE udpBytesReceived;
00020 
00021 //Create a UDP socket for receiving and one for transmitting
00022 static UDP_SOCKET udpSocketUserRx = INVALID_UDP_SOCKET;
00023 static UDP_SOCKET udpSocketUserTx = INVALID_UDP_SOCKET;
00024 
00025 
00026 void main(void)
00027 {
00028     BYTE c;
00029     NODE_INFO udpServerNode;
00030 
00031     //Initialize remote IP and MAC address of udpServerNode with 0, seeing that
00032     //we don't know them for the node that will send us an UDP message. The first
00033     //time a message is received addressed to this port, the remote IP and MAC
00034     //addresses are automatically updated with the addresses of the remote node.
00035     memclr(&udpServerNode, sizeof(udpServerNode));
00036 
00037     //Configure for local port 54123 and remote port INVALID_UDP_PORT. This opens
00038     //the socket to listed on the given port.
00039     udpSocketUserRx = UDPOpen(54123, &udpServerNode, INVALID_UDP_PORT);
00040     
00041     //An error occurred during the UDPOpen() function
00042     if (udpSocketUserRx == INVALID_UDP_SOCKET) {
00043         //Take any additional action that is required when an error occurs
00044     }
00045 
00046     //Infinite loop. Check if anything is received on UDP port
00047     while(1)
00048     {
00049         //Is there any data waiting for us on the UDP socket?
00050         //Because of the design of the Modtronix TCP/IP stack we have to consume
00051         //all data sent to us as soon as we detect it. Store all data to a buffer
00052         //as soon as it is detected
00053         if (UDPIsGetReady(udpSocketUserRx)) {
00054 
00055             udpBytesReceived = UDPGetArray(udpRxBuf, 256);
00056         
00057             //Discard the socket buffer.
00058             UDPDiscard();
00059 
00060             //Check if the UDP socket for transmitting has been created yet. We
00061             //only create this socket after we have received a UDP datagram from
00062             //the client. The reason is that we use the IP and MAC address
00063             //from the received UDP datagram
00064             if (udpSocketUserTx == INVALID_UDP_SOCKET) {
00065                 //Create UDP socket for sending data to remote client on port 54124.
00066                 //We use the UDPGetNodeInfo() go get the IP and MAC address of the
00067                 //remote client. Configure for local port 54130 and remote port 54124.
00068                 udpSocketUserTx = UDPOpen((WORD)54130, UDPGetNodeInfo(), (WORD)54124);
00069     
00070                 //An error occurred during the UDPOpen() function
00071                 if (udpSocketUserTx == INVALID_UDP_SOCKET) {
00072                     //Take any additional action that is required when an error occurs
00073                 }
00074             }
00075         }
00076 
00077 
00078         //Does the UDP receive buffer contain any data?
00079         if(udpBytesReceived > 0) {
00080             //Checks if there is a transmit buffer ready for accepting data, and
00081             //that the given socket is valid (not equal to INVALID_UDP_SOCKET for example)
00082             if (UDPIsPutReady(udpSocketUserTx)) {
00083             
00084                 //Send all bytes received
00085                 UDPPutArray(udpRxBuf, udpBytesReceived);
00086 
00087                 udpBytesReceived = 0;  //Indicate all bytes have been sent
00088             
00089                 // Now send it.
00090                 UDPFlush();
00091             }
00092         }
00093 
00094         //This task performs normal stack task including checking for incoming packet,
00095         //type of packet and calling appropriate stack entity to process it.
00096         StackTask();
00097     }
00098 }

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