Skip to main content

TCP Socket Programming on VxWorks 7

·1051 words·5 mins
VxWorks 7 Socket TCP
Table of Contents
APP - This article is part of a series.
Part 13: This Article

Introduction
#

VxWorks 7 is a powerful real-time operating system (RTOS) widely used in embedded systems, particularly those requiring high levels of real-time performance and stability. VxWorks 7 supports the TCP/IP protocol stack, making it suitable for network communication and various network applications. In this article, we will explore how to perform TCP network programming on VxWorks 7, helping developers understand and implement TCP-based communication.

Development Environment Setup
#

Before diving into TCP network programming on VxWorks 7, we need to ensure that the development environment is set up and configured correctly:

Necessary Software Tools:

  • VxWorks 7: Make sure that VxWorks 7 and Wind River Workbench are installed, as they provide the integrated development environment for application development and debugging.
  • VxWorks Network Protocol Stack: VxWorks 7 comes with an integrated TCP/IP protocol stack, which only needs to be enabled in the Board Support Package (BSP) configuration.
  • Target Hardware: You will need a development board or virtual machine with network interfaces (such as an Ethernet adapter) to test the network functionality.

Steps for Setting Up the Environment:

  1. Install VxWorks 7 and Workbench: Ensure that you have both VxWorks 7 and Wind River Workbench installed on your development machine.
  2. Enable the Network Protocol Stack: By default, VxWorks supports the TCP/IP protocol stack, but you will need to enable this option when creating the BSP for your platform.
  3. Connect to a Network: Connect your target device (e.g., development board) to your development host via Ethernet, Wi-Fi, or use a simulator for testing.

Basic TCP Network Programming
#

VxWorks 7 uses the BSD socket API for network programming, which adheres to the standard TCP/IP protocol, allowing you to create both client and server applications.

Key Socket APIs:

  • socket(): Creates a socket.
  • bind(): Binds a socket to a local address and port.
  • listen(): Prepares the server socket to listen for connection requests.
  • accept(): Accepts a client connection request.
  • connect(): Connects a client to a server.
  • send() and recv(): Send and receive data.
  • close(): Closes the socket.

These APIs are available on VxWorks 7 and allow developers to perform TCP socket communication.

Simple TCP Server Programming Example
#

Below is a basic TCP server program that demonstrates how to use socket programming to create a simple TCP server on VxWorks 7. The server listens on a specific port, accepts client connection requests, and receives messages from the client.

#include <vxWorks.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SERVER_PORT 12345
#define BUFFER_SIZE 1024

int main() {
    int server_sock, client_sock;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_len = sizeof(client_addr);
    char buffer[BUFFER_SIZE];
    int n;

    // Create server socket
    server_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (server_sock < 0) {
        perror("socket() failed");
        return -1;
    }

    // Set up server address
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Accept connections from all network interfaces
    server_addr.sin_port = htons(SERVER_PORT);

    // Bind socket to the address
    if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        perror("bind() failed");
        close(server_sock);
        return -1;
    }

    // Listen for incoming client connections
    if (listen(server_sock, 5) < 0) {
        perror("listen() failed");
        close(server_sock);
        return -1;
    }

    printf("Server is listening on port %d...\n", SERVER_PORT);

    // Accept client connection
    client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &client_len);
    if (client_sock < 0) {
        perror("accept() failed");
        close(server_sock);
        return -1;
    }

    printf("Client connected: %s\n", inet_ntoa(client_addr.sin_addr));

    // Receive data from client
    while (1) {
        memset(buffer, 0, sizeof(buffer));
        n = recv(client_sock, buffer, sizeof(buffer), 0);
        if (n <= 0) {
            printf("Connection closed or error occurred\n");
            break;
        }

        printf("Received from client: %s\n", buffer);

        // Send the received message back to the client
        send(client_sock, buffer, n, 0);
    }

    // Close sockets
    close(client_sock);
    close(server_sock);

    return 0;
}

Code Explanation:

  • Create Socket: The socket() function creates a TCP socket.
  • Bind Address: The bind() function binds the socket to a local IP address and port, so the server can listen on a specific port.
  • Listen for Connections: The listen() function prepares the server socket to listen for incoming client connections.
  • Accept Connection: The accept() function accepts an incoming connection and returns a new socket to communicate with the client.
  • Receive and Send Data: The recv() function receives data from the client, and send() sends the received data back to the client (echo server).
  • Close Socket: After communication ends, the close() function closes both the client and server sockets.

TCP Client Programming Example
#

Next, here’s a simple TCP client program that connects to the server and sends data.

#include <vxWorks.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SERVER_IP "192.168.1.100"  // Server IP address
#define SERVER_PORT 12345
#define BUFFER_SIZE 1024

int main() {
    int client_sock;
    struct sockaddr_in server_addr;
    char buffer[BUFFER_SIZE];
    int n;

    // Create client socket
    client_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (client_sock < 0) {
        perror("socket() failed");
        return -1;
    }

    // Set up server address
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVER_PORT);
    server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);

    // Connect to the server
    if (connect(client_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        perror("connect() failed");
        close(client_sock);
        return -1;
    }

    printf("Connected to server %s:%d\n", SERVER_IP, SERVER_PORT);

    // Send data to the server
    while (1) {
        printf("Enter message to send: ");
        fgets(buffer, sizeof(buffer), stdin);
        if (strcmp(buffer, "exit\n") == 0) break;  // Exit condition

        send(client_sock, buffer, strlen(buffer), 0);

        // Receive echoed message from server
        memset(buffer, 0, sizeof(buffer));
        n = recv(client_sock, buffer, sizeof(buffer), 0);
        if (n <= 0) {
            printf("Server closed connection\n");
            break;
        }
        printf("Received from server: %s\n", buffer);
    }

    // Close socket
    close(client_sock);

    return 0;
}

Code Explanation:

  • Create Socket: The socket() function creates a TCP socket for the client.
  • Connect to Server: The connect() function connects the client to the server’s IP and port.
  • Send and Receive Data: The send() function sends a message to the server, while the recv() function receives the server’s echoed message.
  • Close Socket: After communication ends, the close() function closes the client socket.

Conclusion
#

In this article, we demonstrated how to perform basic TCP socket programming on VxWorks 7, creating a simple TCP server and client. VxWorks 7 provides a robust network protocol stack that allows you to use the BSD socket API for network communication, making it similar to traditional Linux/Unix systems. Developers can easily port existing network code to VxWorks.

If you need to implement more complex communication using TCP, you can extend this basic example with features like multithreading, SSL encryption, timeout handling, etc.

APP - This article is part of a series.
Part 13: This Article

Related

Enabling Qt on VxWorks
·898 words·5 mins
VxWorks 7 QT
Deploying a VxWorks RTP as a Container to DockerHub and Raspberry Pi 4
·757 words·4 mins
VxWorks 7 RTP Container Raspberry Pi 4
VxWorks 7 BSP Development Guide
·1720 words·9 mins
VxWorks 7 Workbench