0Pricing
C Academy · Lesson

TCP Server

Accept connections.

Building a TCP Server

A TCP server waits for clients and accepts their connections. The lifecycle is: create a socket, bind it to a port, listen, then accept connections in a loop.

Step 1: Create the Socket

Create a stream socket for IPv4 TCP. A negative result means failure.

#include <sys/socket.h>
#include <stdio.h>

int make_server_socket(void) {
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0) { perror("socket"); }
    return fd;
}

All lessons in this course

  1. Sockets Overview
  2. TCP Server
  3. TCP Client
  4. Handling Multiple Clients
← Back to C Academy