User Datagram Protocol is an alternative way of sending/receive data through the internet without establishing a connection. Unlike TCP, UDP is unreliable because the receiver is not guaranteed to get the data. In addition, out of order packets may occur in the receiver's side.
As a result, UDP is mostly used in video and audio streaming because delayed packets effect the playback quality( i.e. lagging) while dropped packets do not effect the playback quality as much as delayed packets. Another unique UDP's characteristic is the ability to broadcast and multicast to different users.
The Other Day I wrote a program for testing our new W7100 in UDP mode. This program basically sends out a reply message whenever the W7100 receives a UDP message from a UDP client.
There are some considerations when writing this program:
1) How long does the Ethernet negotiation take place?
Currently, I have delayed the program to start after 7 seconds has elapsed
Without this delay, the program will go wild!
2) How much buffer is needed when receiving ?
The buffer string size is the same size as the RX buffer in the W7100 socket.
I hope you enjoyed my article. Enjoy Programming!
Below is the code which I have used to perform this test.
#define MAX_BUF_SIZE 2048 // define maximum buffer size
uint8 xdata pDestaddr[4]; //xdata using external ram
uint16 xdata pPort; // destination port
uint8 xdata rcvLen; // length of data received
uint16 xdata length; // the length of data received
uint8 xdata RXdata[MAX_BUF_SIZE]; // RX data received
uint8 xdata TXData[MAX_BUF_SIZE]; // TX data send
uint16 xdata sentLen; // length of data sent
uint16 xdata TXSize; // size of string sent
Init_iMCU(); // Initialize iMCUW7100
Init_Network(); // Initialize Network Configuration
//Delay for link
wait_1ms(7000);
// 1. create socket for udp
if(socket(0, Sn_MR_UDP,5000, 0) == 1) // create socket with UDP port 5000
{
PutString("\r\n opened socket");
// 2. receive data
while(getSn_RX_RSR(0) == 0) ; // check if there is data in socket
PutString("\r\n got data");
rcvLen = getSn_RX_RSR(0);
if(rcvLen > MAX_BUF_SIZE) // check if data received is bigger than size of buffer string
rcvLen = MAX_BUF_SIZE; // assign max buffer size to the received length
PutString("\r\n RX data size is "); PutITOA(rcvLen); PutString(" Bytes.");
length = recvfrom(0, RXdata, rcvLen, pDestaddr, &pPort); // receive data
PutString("\r\n Received Data");
RXdata[length] = 0; // indicate the end of string
PutString("\r\n RX data : ");
PutString(RXdata); // print received data
// 3. trasnmit data
//set Dest. IP address & Port
pDestaddr[0]= 192;
pDestaddr[1]= 168;
pDestaddr[2]= 1;
pDestaddr[3]= 3;
pPort = 5000;
// Send reply string 'HELLO'
PutString("\r\n TX data size is ");PutITOA(TXSize);
sentLen = sendto(0, TXData, TXSize, pDestaddr, pPort); // transfer data
if (sentLen == TXSize)
PutString("\r\n success to transmit");
else
PutString("\r\n Failed to transmit");
close(0); // close the socket
}
else // if socket is fail
PutString("\r\n Failed to open socket");
while(1);