π 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 #
-
UDP Server (
udpServerTask)- Creates a socket with
SOCK_DGRAM. - Binds to port 7000.
- Waits for datagrams using
recvfrom(). - Echoes the message back with
sendto().
- Creates a socket with
-
UDP Client (
udpClientTask)- Creates a socket.
- Sends a message to the server using
sendto(). - Waits for the echoed response with
recvfrom().
-
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()andrecvfrom(). - 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.β