0Pricing
C Academy · Lesson

TCP Client

Connect and send.

Building a TCP Client

A TCP client connects to a server, then sends and receives data. Its lifecycle is shorter than the server's: create a socket, connect, communicate, and close.

Step 1: Create the Socket

Just like the server, create a stream socket for IPv4 TCP.

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

int make_client_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