TCP Echo Server
A TCP echo server is a simple network program that accepts TCP connections from clients and sends back exactly the same data it receives. This behavior is called echoing.
In a TCP echo setup, the server waits for incoming client connections. When a client connects and sends a message, the server immediately sends that message back to the client. The client then reads and displays the echoed response. This example is widely used to learn socket programming because it demonstrates all core steps of TCP communication: creating sockets, binding to an address, listening for connections, accepting clients, receiving data, and sending responses.
echo_server.c
echo_client.c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 12345 #define BUFFER_SIZE 1024 int main() { int server_fd, client_fd; struct sockaddr_in server_addr, client_addr; socklen_t addr_len = sizeof(client_addr); char buffer[BUFFER_SIZE]; ssize_t bytes_read; // Create a TCP socket server_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_fd < 0) { perror("socket failed"); exit(EXIT_FAILURE); } // Set up the server address struct server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; // Listen on any interface server_addr.sin_port = htons(PORT); // Bind the socket to the specified port and address if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { perror("bind failed"); close(server_fd); exit(EXIT_FAILURE); } // Listen for incoming connections if (listen(server_fd, 1) < 0) { perror("listen failed"); close(server_fd); exit(EXIT_FAILURE); } printf("Echo server listening on port %d...\n", PORT); // Accept a client connection client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len); if (client_fd < 0) { perror("accept failed"); close(server_fd); exit(EXIT_FAILURE); } printf("Client connected.\n"); // Echo loop: receive data and send it back while ((bytes_read = recv(client_fd, buffer, BUFFER_SIZE, 0)) > 0) { // Send the received data back to the client send(client_fd, buffer, bytes_read, 0); } printf("Client disconnected.\n"); close(client_fd); close(server_fd); return 0; }
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain the steps involved in setting up a TCP echo server?
What are some common issues that can occur in a TCP echo setup?
Can you provide a simple example of a TCP echo server and client?
Awesome!
Completion rate improved to 8.33
TCP Echo Server
Swipe to show menu
A TCP echo server is a simple network program that accepts TCP connections from clients and sends back exactly the same data it receives. This behavior is called echoing.
In a TCP echo setup, the server waits for incoming client connections. When a client connects and sends a message, the server immediately sends that message back to the client. The client then reads and displays the echoed response. This example is widely used to learn socket programming because it demonstrates all core steps of TCP communication: creating sockets, binding to an address, listening for connections, accepting clients, receiving data, and sending responses.
echo_server.c
echo_client.c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 12345 #define BUFFER_SIZE 1024 int main() { int server_fd, client_fd; struct sockaddr_in server_addr, client_addr; socklen_t addr_len = sizeof(client_addr); char buffer[BUFFER_SIZE]; ssize_t bytes_read; // Create a TCP socket server_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_fd < 0) { perror("socket failed"); exit(EXIT_FAILURE); } // Set up the server address struct server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; // Listen on any interface server_addr.sin_port = htons(PORT); // Bind the socket to the specified port and address if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { perror("bind failed"); close(server_fd); exit(EXIT_FAILURE); } // Listen for incoming connections if (listen(server_fd, 1) < 0) { perror("listen failed"); close(server_fd); exit(EXIT_FAILURE); } printf("Echo server listening on port %d...\n", PORT); // Accept a client connection client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len); if (client_fd < 0) { perror("accept failed"); close(server_fd); exit(EXIT_FAILURE); } printf("Client connected.\n"); // Echo loop: receive data and send it back while ((bytes_read = recv(client_fd, buffer, BUFFER_SIZE, 0)) > 0) { // Send the received data back to the client send(client_fd, buffer, bytes_read, 0); } printf("Client disconnected.\n"); close(client_fd); close(server_fd); return 0; }
Thanks for your feedback!