C TCP Server/Client: A Deep Dive 🎯

beginner
7 min

C TCP Server/Client: A Deep Dive 🎯

Welcome to our comprehensive guide on creating a TCP Server and Client in C! In this lesson, we'll explore the world of network programming, focusing on the Client-Server model. By the end of this tutorial, you'll be able to build your own TCP applications, opening doors to various real-world projects.

Let's get started! 📝

Table of Contents

  1. Introduction to TCP and Sockets

    • What is TCP?
    • What are Sockets?
    • TCP vs UDP
  2. Setting up the Development Environment

    • Installing a C Compiler
    • Understanding Netcat (used for testing)
  3. Creating a Simple TCP Server

    • Building the Server Code
    • Understanding the Server Code
  4. Connecting to the Server: A TCP Client

    • Building the Client Code
    • Understanding the Client Code
  5. Advanced Server Features

    • Multiple Client Connections
    • Handling Client Requests
  6. Building a Real-world Application: Chat Server

    • Designing the Server and Client
    • Implementing the Chat Functionality
  7. Quiz Time! 💡


1. Introduction to TCP and Sockets

TCP (Transmission Control Protocol) is a protocol used to send and receive data over the Internet. It ensures reliable data transmission by breaking the data into small packets, which are then sent over the network.

Sockets are the endpoints of a two-way communication link between two hosts. They provide a reliable way for a program to send and receive data over a network.

TCP and UDP (User Datagram Protocol) are two main types of Internet protocols. While TCP ensures reliable data transmission, UDP is faster but doesn't guarantee data delivery.


2. Setting up the Development Environment

To work with TCP servers and clients in C, you'll need a C compiler. We recommend using GCC (GNU Compiler Collection).

Netcat is a versatile network utility used for reading from and writing to network connections using TCP or UDP. It's particularly useful for testing TCP servers and clients.


3. Creating a Simple TCP Server

Let's create a basic TCP server that accepts connections and sends a simple message to the client.

c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> // Function to create a socket int create_socket(int port) { int sockfd, optval = 1; struct sockaddr_in addr; // Create the socket sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket creation error"); exit(EXIT_FAILURE); } // Set options for the socket setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); // Set up the address structure addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(port); // Bind the socket to the address if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind error"); exit(EXIT_FAILURE); } // Listen for incoming connections listen(sockfd, 10); return sockfd; } // Main function int main() { int server_sockfd, client_sockfd; char buffer[1024]; // Create the server socket server_sockfd = create_socket(54000); // Accept an incoming connection printf("Waiting for a client connection...\n"); client_sockfd = accept(server_sockfd, NULL, NULL); if (client_sockfd < 0) { perror("accept error"); exit(EXIT_FAILURE); } // Read the message from the client read(client_sockfd, buffer, sizeof(buffer)); printf("Received message: %s\n", buffer); // Send a message to the client write(client_sockfd, "Hello, Client!", strlen("Hello, Client!")); return 0; }

4. Connecting to the Server: A TCP Client

Now let's create a simple TCP client that connects to the server and sends a message.

c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> // Function to create a socket int create_socket(int port) { int sockfd; struct sockaddr_in addr; // Create the socket sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket creation error"); exit(EXIT_FAILURE); } // Set up the address structure addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Connect to the server if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("connection error"); exit(EXIT_FAILURE); } return sockfd; } // Main function int main() { int sockfd; char message[] = "Hello Server!"; char buffer[1024]; // Create the client socket sockfd = create_socket(54000); // Send the message to the server write(sockfd, message, strlen(message)); // Read the response from the server read(sockfd, buffer, sizeof(buffer)); printf("Received message: %s\n", buffer); return 0; }

Continue to Advanced Server Features 📝


Quick Quiz
Question 1 of 1

Which protocol does TCP ensure reliable data transmission over the Internet?