TCP Socket Programming in Unix Using C Programming
On this page (8sections)
TCP Socket Programming in Unix Using C Programming
This tutorial demonstrates a simple TCP echo client–server in C on a Unix-like system. The server listens on a port, accepts a client connection, reads a message, and sends it back. The client connects, sends text, and prints the echoed response.
Introduction
TCP (Transmission Control Protocol) is a connection-oriented transport protocol. It provides reliable, ordered delivery between applications. Socket programming uses the BSD socket API (socket, bind, listen, accept, connect, read/write) to exchange data over TCP/IP.
Concept
| Role | Steps |
|---|---|
| Server | Create socket → bind to IP/port → listen → accept client → read/write → close |
| Client | Create socket → connect to server → write message → read response → close |
TCP is stream-oriented: one read() may not return a full message. For production code, use length prefixes or delimiters. This example keeps messages small for clarity.
TCP Echo Server
Replace 127.0.0.1 and port 1012 with values suitable for your lab environment.
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main(void) {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
char buffer[1024] = {0};
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("socket");
return 1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(1012);
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
return 1;
}
if (listen(server_fd, 5) < 0) {
perror("listen");
return 1;
}
printf("Server listening on port 1012...\n");
client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
if (client_fd < 0) {
perror("accept");
return 1;
}
ssize_t n = read(client_fd, buffer, sizeof(buffer) - 1);
if (n > 0) {
buffer[n] = '\0';
printf("Received: %s\n", buffer);
write(client_fd, buffer, n);
}
close(client_fd);
close(server_fd);
return 0;
}
Server explanation
socket(AF_INET, SOCK_STREAM, 0)creates a TCP socket.bind()attaches the socket to127.0.0.1:1012.listen()marks the socket as passive (accepting connections).accept()blocks until a client connects and returns a new connected socket.read()receives data;write()sends the same bytes back (echo).
Compile and run the server first:
gcc -o tcp_server tcp_server.c
./tcp_server
TCP Echo Client
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main(void) {
int sock;
struct sockaddr_in server_addr;
char message[1024] = "Hello TCP Server";
char response[1024] = {0};
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket");
return 1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(1012);
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
return 1;
}
write(sock, message, strlen(message));
ssize_t n = read(sock, response, sizeof(response) - 1);
if (n > 0) {
response[n] = '\0';
printf("Echo from server: %s\n", response);
}
close(sock);
return 0;
}
Client explanation
- Create a TCP socket with the same address family as the server.
connect()initiates the three-way handshake to the server.write()sends the message;read()receives the echoed reply.
In a second terminal (while the server is running):
gcc -o tcp_client tcp_client.c
./tcp_client
Expected output on the client:
Echo from server: Hello TCP Server
Running Both Programs
- Start the server in one terminal.
- Start the client in another terminal on the same machine (or update the IP if remote).
- Confirm the server prints the received message and the client prints the echo.
For multiple clients, loop accept() in the server and use fork(), threads, or select()/poll() — see TCP Chat Client/Server Programming.
Related Tutorials
Related Tutorials
Unix Basic Commands
To create a new file and add content to it, you can use the cat command with the output redirection operator ().
Read tutorialFIFO Client/Server Program in Unix Using C Programming
In this blog post, we will explore the implementation of a FIFO (First-In-First-Out) client/server program in the Unix environment using the C programming langu
Read tutorialMessage Queue in Unix Using C Programming
Message queues are a crucial inter-process communication mechanism used in Unix-based systems for sending and receiving messages between processes. They provide
Read tutorial