UDP Socket Tutorial

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial will show you how to achieve a consistent UDP data exchange using our modules.

UDP Protocol

UDP (User Data Protocol) is a packet oriented protocol.

  • UDP is a connectionless protocol (you cannot know if/when a client is connected to a server for example....)
  • Packets can't be reordered and recovered with acknowledgement checks
  • Less protocol data bytes than a TCP Packet
  • Checksum error detection
  • Can perform a Server to Multi-Client transmission

What does "UDP is connectionless" mean?

UDP is connectionless in the sense that Server does not worry about the Clients, and it sends information even if no clients are connected. Using TCP a connection must exists between server and client, using UDP this kind of connection is not required. In this way, if a client close its socket, server cannot "understand" it and could continue to send data!

What is the difference between a UDP Server and a UDP Broadcast Server?

A Simple UDP Server is made to send data to only one client at time, and can receive data. In fact, Flyport UDP Server can send data to a client only if it was the last udp client that sent some data.
For example if Flyport 1 is server, Flyport 2 and 3 are clients, Flyport 1 can send data to Flyport 2 only if Flyport 2 is last client that sent data to server. If Flyport 2 send data and after Flyport 3 send data, Flyport 1 can send data only to Flyport 3.

A UDP Broadcast Server can send data to multiple clients, but it cannot receive data from clients! It is a sort-of one way connection.
A simple example is Internet Radio Service, where the stream of data is always available even if no clients are present, but clients cannot send data to server!

UDP Server Socket usage example: use Flyport as UDP Server and a PC as client

Inizial setup

To create a Simple UDP Server example you can start creating a new project for your Flyport Wi-fi or Ethernet. If needed, you can add more space to each UDP socket Buffer using the IDE Wizard. NOTE that at least one UDP Socket should be enabled to let this example code works properly!
Install the useful "TCP/IP Builder" software on your pc and open it! Fill params with your PC local network IP Address, and use port 5010 as port, choose UDP Protocol.
In this way you are creating a UDP socket that will be our UDP client.

Code example

In the following code example you can choose to use UDPServer or UDPBroadcast type socket. You can act as follows:

  • To enable UDPBroadcast uncomment line // UdpSocket = UDPServerOpen("5010"); and comment UdpSocket = UDPBroadcastOpen("5010");
  • To enable UDPServer uncomment line UdpSocket = UDPServerOpen("5010"); and comment // UdpSocket = UDPBroadcastOpen("5010");


NOTE: in case you choose UDPBroadcast, the code to check and read Server Rx buffer Length will not read any data!

#include "taskFlyport.h"
 
int UdpSocket;
int UdpRxLength = 0;
char UdpString [512]; 
 
void FlyportTask()
{
    // Flyport connects to default network
    WFConnect(WF_DEFAULT);
    while(WFGetStat() != CONNECTED);
    vTaskDelay(25);
 
    UARTWrite(1,"Flyport connected... hello world!\r\n");
 
    // Choose to create a UDPServer connection for a point-point data exchange
    // or create a UDPBroadcast connection for a one-way, multipoint data exchange
 
    // Open Server UDP connection, port 5010
    UdpSocket = UDPServerOpen("5010");
    // or
    // Open a Broadcast Server UDP connection, port 5010
    // UdpSocket = UDPBroadcastOpen("5010");
 
    while(1)
    {
        // wait 0.5 sec
        vTaskDelay(50);
 
        if(!UdpSocket)
        {
            UARTWrite(1, "unable to open server UDP socket\r\n");
        }
        else
        {
            // write a string via UDP!
            UDPWrite(UdpSocket, (BYTE*)"Hello!\r\n", 8);
 
            // Check Server RX length
            UdpRxLength = UDPRxLen(UdpSocket);
 
            // Check if server received some data: will not work for UDPBroadcast
            if(UdpRxLength > 0)
            {
                sprintf(UdpString, "UDP Received: %d BYTES\r\n", UdpRxLength);
                UARTWrite(1, UdpString);
                vTaskDelay(20);
                UDPRead(UdpSocket, UdpString, UdpRxLength);
                UdpString[UdpRxLength] = '\0';
                UARTWrite(1, UdpString);
                UARTWrite(1,"\r\n");
            }
        }
    }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox