Skip to main content

UDP Networking in VxWorks: Sending and Receiving Packets

·574 words·3 mins
VxWorks RTOS Networking Sockets UDP Embedded Systems Programming Tutorial
Table of Contents
VxWorks Programming Tutorial for Beginners - This article is part of a series.
Part 9: This Article

πŸš€ Introduction
#

In the previous blog Handling Multiple Clients in VxWorks with select, we learned how to handle multiple clients with select() in a TCP server.

Now it’s time to look at UDP (User Datagram Protocol) β€” a faster, lightweight alternative to TCP.

UDP is widely used in:

  • Real-time applications (VoIP, gaming).
  • Sensor data streaming.
  • Broadcast/multicast messages.

In this tutorial, you’ll learn:

  • How UDP sockets work in VxWorks.
  • How to build a UDP server and UDP client.
  • When to use UDP instead of TCP.


🧩 UDP vs TCP
#

  • TCP: Reliable, connection-oriented, ensures delivery and order.
  • UDP: Fast, connectionless, no guarantee of delivery or order.
  • Use UDP when speed and low latency are more important than reliability.

πŸ’» Example: UDP Server and Client in VxWorks
#

We’ll create:

  • A UDP server that listens on port 7000.
  • A UDP client that sends messages to the server.

Code Example
#

#include <vxWorks.h>
#include <taskLib.h>
#include <sockLib.h>
#include <inetLib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define SERVER_PORT 7000
#define SERVER_IP   "127.0.0.1"   // Localhost for test

// UDP Server Task
void udpServerTask()
{
    int sock;
    struct sockaddr_in serverAddr, clientAddr;
    char buffer[256];
    int addrLen = sizeof(clientAddr);

    sock = socket(AF_INET, SOCK_DGRAM, 0);

    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = INADDR_ANY;
    serverAddr.sin_port = htons(SERVER_PORT);

    bind(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr));

    printf("UDP Server: Listening on port %d\n", SERVER_PORT);

    while (1)
    {
        int bytes = recvfrom(sock, buffer, sizeof(buffer)-1, 0,
                             (struct sockaddr*)&clientAddr, &addrLen);
        if (bytes > 0)
        {
            buffer[bytes] = '\0';
            printf("UDP Server: Received -> %s\n", buffer);

            // Echo back to client
            sendto(sock, buffer, bytes, 0,
                   (struct sockaddr*)&clientAddr, addrLen);
        }
    }
}

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1543398821442998"
     crossorigin="anonymous"></script>
<!-- vxworks6_ads_2 -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-1543398821442998"
     data-ad-slot="4807616072"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

// UDP Client Task
void udpClientTask()
{
    int sock;
    struct sockaddr_in serverAddr;
    char *msg = "Hello from UDP Client";
    char buffer[256];
    int addrLen = sizeof(serverAddr);

    sock = socket(AF_INET, SOCK_DGRAM, 0);

    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
    serverAddr.sin_port = htons(SERVER_PORT);

    sendto(sock, msg, strlen(msg), 0,
           (struct sockaddr*)&serverAddr, addrLen);

    int bytes = recvfrom(sock, buffer, sizeof(buffer)-1, 0,
                         (struct sockaddr*)&serverAddr, &addrLen);
    if (bytes > 0)
    {
        buffer[bytes] = '\0';
        printf("UDP Client: Received -> %s\n", buffer);
    }
}

// Init function
void usrAppInit(void)
{
    taskSpawn("tUdpServer", 100, 0, 4000, (FUNCPTR)udpServerTask,
              0,0,0,0,0,0,0,0,0,0);

    taskDelay(100); // Delay so server starts first

    taskSpawn("tUdpClient", 150, 0, 4000, (FUNCPTR)udpClientTask,
              0,0,0,0,0,0,0,0,0,0);
}

πŸ“ Explanation of the Code
#

  1. UDP Server (udpServerTask)

    • Creates a socket with SOCK_DGRAM.
    • Binds to port 7000.
    • Waits for datagrams using recvfrom().
    • Echoes the message back with sendto().
  2. UDP Client (udpClientTask)

    • Creates a socket.
    • Sends a message to the server using sendto().
    • Waits for the echoed response with recvfrom().
  3. usrAppInit()

    • Spawns both server and client tasks for demo purposes.

⚑ What You’ll See
#

When running, the output looks like:

UDP Server: Listening on port 7000
UDP Server: Received -> Hello from UDP Client
UDP Client: Received -> Hello from UDP Client

πŸ” Key Takeaways
#

  • UDP is fast and lightweight, ideal for real-time and broadcast.
  • No connection setup β€” just sendto() and recvfrom().
  • Reliability is not guaranteed β€” applications must handle lost packets if needed.

βœ… Wrap-Up
#

In this tutorial, you learned:

  • The difference between TCP and UDP in VxWorks.
  • How to implement UDP server and client tasks.
  • When to use UDP for embedded networking.

In the next blog, we’ll explore inter-task communication using message queues in VxWorks, a powerful way to pass messages between tasks without sockets.


πŸ‘‰ Stay tuned for Blog 10: β€œInter-Task Communication with Message Queues in VxWorks.”

VxWorks Programming Tutorial for Beginners - This article is part of a series.
Part 9: This Article

Related

Handling Multiple Clients in VxWorks with select
·648 words·4 mins
VxWorks RTOS Networking Sockets Select() Multi-Client Embedded Systems Programming Tutorial
Networking with VxWorks: Socket Programming Basics
·578 words·3 mins
VxWorks RTOS Networking Sockets TCP/IP Embedded Systems Programming Tutorial
Interrupt Handling in VxWorks Device Drivers
·497 words·3 mins
VxWorks RTOS Interrupts Device Driver Embedded Systems Programming Tutorial