A Simple C TCP Server and Client Example for Byte Array Transfer

Dec 06, 2025 · Programming · 11 views · 7.8

Keywords: TCP | C | server | client | socket

Abstract: Based on Beej's Guide to Network Programming, this article presents a simplified C implementation of a TCP server and client designed for transferring byte arrays between computers. It includes code examples, compilation instructions, and tips for C++ compatibility, suitable for quick learning.

Introduction

In response to a common need for a minimal TCP server and client in C or C++ to transfer byte arrays, this article provides a simplified approach using standard BSD sockets. The solution avoids third-party libraries and focuses on core concepts for educational purposes.

TCP Socket Fundamentals

TCP (Transmission Control Protocol) provides reliable, connection-oriented communication. In C, socket programming involves creating, binding, listening, accepting, connecting, sending, and receiving data through file descriptors.

Server Implementation

// Simplified C server code based on Beej's Guide
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
    int sockfd, newsockfd;
    struct sockaddr_in serv_addr, cli_addr;
    socklen_t clilen;
    char buffer[256];
    int n;

    // Create socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) error("ERROR opening socket");

    // Initialize server address
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(8080); // Port number

    // Bind socket
    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
        error("ERROR on binding");

    // Listen for connections
    listen(sockfd, 5);
    clilen = sizeof(cli_addr);

    // Accept connection
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
    if (newsockfd < 0) error("ERROR on accept");

    // Read data from client
    bzero(buffer, 256);
    n = read(newsockfd, buffer, 255);
    if (n < 0) error("ERROR reading from socket");
    printf("Message from client: %s\n", buffer);

    // Send response
    n = write(newsockfd, "I got your message", 18);
    if (n < 0) error("ERROR writing to socket");

    // Close sockets
    close(newsockfd);
    close(sockfd);
    return 0;
}

This server listens on port 8080, accepts one connection, reads a message, and sends a response. Error handling is simplified for brevity.

Client Implementation

// Simplified C client code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

int main(int argc, char *argv[]) {
    int sockfd;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    char buffer[256];
    int n;

    if (argc < 3) {
        fprintf(stderr,"usage %s hostname port\n", argv[0]);
        exit(0);
    }

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) error("ERROR opening socket");

    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(atoi(argv[2]));

    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) 
        error("ERROR connecting");

    printf("Please enter the message: ");
    bzero(buffer, 256);
    fgets(buffer, 255, stdin);
    n = write(sockfd, buffer, strlen(buffer));
    if (n < 0) error("ERROR writing to socket");

    bzero(buffer, 256);
    n = read(sockfd, buffer, 255);
    if (n < 0) error("ERROR reading from socket");
    printf("%s\n", buffer);

    close(sockfd);
    return 0;
}

The client connects to a specified host and port, sends a message, and receives a response.

Compilation and Execution

To compile, use gcc: gcc server.c -o server and gcc client.c -o client. Run the server first, then the client with appropriate arguments.

C++ Compatibility Considerations

For C++ compilation, as noted in the answers, add headers like &lt;cstdlib&gt; and &lt;cstring&gt;, and ensure type casts, such as changing clilen to socklen_t and using explicit cast in connect.

Conclusion

This example provides a foundational understanding of TCP socket programming in C, suitable for simple byte array transfer. For more details, refer to Beej's Guide to Network Programming.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.