ex_udp_client.c

This example program shows how to use the TCP/IP stack in client mode. It sends a single UDP datagram after the server's MAC address has been resolved via ARP. The datagram is sent to socket 10.1.0.101:54124 (IP address 10.1.0.101, port 54124).

00001 
00009 #include "net\stacktsk.h"
00010 #include "net\tick.h"
00011 #include "net\helpers.h"
00012 #include "net\udp.h"
00013 #include "net\arp.h"
00014 #include "net\arptsk.h"
00015 
00016 //Create a UDP socket for receiving and sending data
00017 static UDP_SOCKET udpSocketUser;
00018 
00019 //UDP State machine
00020 #define SM_UDP_SEND_ARP     0
00021 #define SM_UDP_WAIT_RESOLVE 1
00022 #define SM_UDP_RESOLVED     2
00023 #define SM_UDP_SENT         3
00024 
00025 static BYTE smUdp = SM_UDP_SEND_ARP;
00026 
00027 //Timers
00028 TICK8  tsecWait = 0;    //General purpose wait timer
00029 
00030 void udpExample(void)
00031 {
00032     BYTE c;
00033     NODE_INFO udpServerNode;
00034 
00035     //Initialize remote IP and address with 10.1.0.101. The MAC address is
00036     //is not intialized yet, but after we receive an ARP responce.
00037     //Configure for local port 54123 and remote port 54124.
00038     udpServerNode.IPAddr.v[0] = 10;
00039     udpServerNode.IPAddr.v[1] = 1;
00040     udpServerNode.IPAddr.v[2] = 0;
00041     udpServerNode.IPAddr.v[3] = 101;
00042     udpSocketUser = UDPOpen(54123, &udpServerNode, 54124);
00043     
00044     //An error occurred during the UDPOpen() function
00045     if (udpSocketUser == INVALID_UDP_SOCKET) {
00046         //Add user code here to take action if required!
00047     }
00048 
00049     //Infinite loop. Check if anything is received on UDP port
00050     while(1)
00051     {
00052         switch (smUdp) {
00053         case SM_UDP_SEND_ARP:
00054             if (ARPIsTxReady()) {
00055                 //Remember when we sent last request
00056                 tsecWait = TickGet8bitSec();
00057                 
00058                 //Send ARP request for given IP address
00059                 ARPResolve(&udpServerNode.IPAddr);
00060                 
00061                 smUdp = SM_UDP_WAIT_RESOLVE;
00062             }
00063             break;
00064         case SM_UDP_WAIT_RESOLVE:
00065             //The IP address has been resolved, we now have the MAC address of the
00066             //node at 10.1.0.101
00067             if (ARPIsResolved(&udpServerNode.IPAddr, &udpServerNode.MACAddr)) {
00068                 smUdp = SM_UDP_RESOLVED;
00069             }
00070             //If not resolved after 2 seconds, send next request
00071             else {
00072                 if (TickGetDiff8bitSec(tsecWait) >= 2) {
00073                     smUdp = SM_UDP_SEND_ARP;
00074                 }
00075             }
00076             break;
00077         case SM_UDP_RESOLVED:
00078             //Checks if there is a transmit buffer ready for accepting data
00079             if (UDPIsPutReady(udpSocketUser)) {
00080                 //Send a UDP Datagram with "Hello"
00081                 UDPPut('H');
00082                 UDPPut('e');
00083                 UDPPut('l');
00084                 UDPPut('l');
00085                 UDPPut('o');
00086 
00087                 //Send contents of transmit buffer, and free buffer
00088                 UDPFlush();
00089         
00090                 smUdp = SM_UDP_SENT;    //Set to "message sent" state
00091             }
00092             break;
00093         case SM_UDP_SENT:
00094             //Message sent state. If the user wants to resend the message, smUdp must be reset
00095             //to SM_UDP_RESOLVED
00096             break;
00097         }
00098 
00099 
00100         //This task performs normal stack task including checking for incoming
00101         //packet, type of packet and calling appropriate stack entity to
00102         //process it.
00103         StackTask();
00104     }
00105 }

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