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! 📝
Introduction to TCP and Sockets
Setting up the Development Environment
Creating a Simple TCP Server
Connecting to the Server: A TCP Client
Advanced Server Features
Building a Real-world Application: Chat Server
Quiz Time! 💡
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.
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.
Let's create a basic TCP server that accepts connections and sends a simple message to the client.
#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;
}Now let's create a simple TCP client that connects to the server and sends a message.
#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 📝
Which protocol does TCP ensure reliable data transmission over the Internet?