🚀 Introduction #
So far, we’ve focused on tasks, synchronization, drivers, and interrupts.
Now let’s connect our embedded system to the outside world with networking.
VxWorks provides a full TCP/IP stack with standard BSD socket APIs, meaning if you know sockets in Linux, you’ll feel right at home.
In this tutorial, you’ll learn:
- How socket programming works in VxWorks.
- How to write a TCP server task.
- How to write a TCP client task.
🧩 What are Sockets? #
- A socket is an endpoint for communication between two systems.
- VxWorks supports both TCP (reliable) and UDP (fast, connectionless).
- APIs are almost identical to standard BSD sockets:
socket()
,bind()
,listen()
,accept()
,connect()
,send()
,recv()
.
💻 Example: TCP Server and Client in VxWorks #
We’ll create:
- A TCP server task that listens on port 5000.
- A TCP client task that connects to it and sends messages.
Code Example #
#include <vxWorks.h>
#include <taskLib.h>
#include <sockLib.h>
#include <inetLib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#define SERVER_PORT 5000
#define SERVER_IP "127.0.0.1" // Loopback for test
// TCP Server Task
void tcpServerTask()
{
int serverSock, clientSock;
struct sockaddr_in serverAddr, clientAddr;
int addrLen = sizeof(clientAddr);
char buffer[128];
serverSock = socket(AF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(SERVER_PORT);
bind(serverSock, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
listen(serverSock, 1);
printf("TCP Server: Listening on port %d\n", SERVER_PORT);
clientSock = accept(serverSock, (struct sockaddr*)&clientAddr, &addrLen);
printf("TCP Server: Client connected\n");
while (1)
{
int bytes = recv(clientSock, buffer, sizeof(buffer)-1, 0);
if (bytes > 0)
{
buffer[bytes] = '\0';
printf("TCP Server: Received -> %s\n", buffer);
send(clientSock, "ACK", 3, 0);
}
}
}
// TCP Client Task
void tcpClientTask()
{
int sock;
struct sockaddr_in serverAddr;
char *msg = "Hello from VxWorks Client";
char buffer[128];
sock = socket(AF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
serverAddr.sin_port = htons(SERVER_PORT);
if (connect(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == 0)
{
printf("TCP Client: Connected to server\n");
send(sock, msg, strlen(msg), 0);
int bytes = recv(sock, buffer, sizeof(buffer)-1, 0);
if (bytes > 0)
{
buffer[bytes] = '\0';
printf("TCP Client: Received -> %s\n", buffer);
}
}
else
{
printf("TCP Client: Failed to connect\n");
}
}
// Init function
void usrAppInit(void)
{
taskSpawn("tServer", 100, 0, 4000, (FUNCPTR)tcpServerTask,
0,0,0,0,0,0,0,0,0,0);
taskDelay(100); // Delay so server is ready
taskSpawn("tClient", 150, 0, 4000, (FUNCPTR)tcpClientTask,
0,0,0,0,0,0,0,0,0,0);
}
📝 Explanation of the Code #
-
TCP Server (
tcpServerTask
)- Creates a socket with
socket()
. - Binds to port 5000 with
bind()
. - Listens for incoming clients with
listen()
. - Accepts a connection with
accept()
. - Receives and sends messages using
recv()
andsend()
.
- Creates a socket with
-
TCP Client (
tcpClientTask
)- Creates a socket.
- Connects to the server using
connect()
. - Sends a message to the server.
- Waits for an ACK response.
-
usrAppInit()
- Spawns both tasks (server and client) for demonstration.
⚡ What You’ll See #
When running, the output will look like:
TCP Server: Listening on port 5000
TCP Client: Connected to server
TCP Server: Client connected
TCP Server: Received -> Hello from VxWorks Client
TCP Client: Received -> ACK
🔍 Key Takeaways #
- VxWorks supports BSD-style sockets, so most Linux socket code works directly.
- You can build servers and clients using TCP or UDP.
- Tasks handle networking just like file I/O in VxWorks.
✅ Wrap-Up #
In this tutorial, you learned:
- The basics of socket programming in VxWorks.
- How to implement a TCP server and client.
- How to send and receive messages between tasks over the network.
In the next blog, we’ll explore multitasking with select() and multiple sockets to handle concurrent clients.
👉 Stay tuned for Blog 8: “Handling Multiple Clients in VxWorks with select().”