Socket Programming in C TCP Client and Server
Learn socket programming in C by building a simple TCP client and server on Debian 12. This tutorial covers creating sockets, connecting, sending, and receiving data with clear code examples using Vim.
Socket Programming in C: TCP Client and Server
TCP Server
The server creates a socket, binds to a port, listens for connections, and accepts one client. It then receives a message and replies back.Server Code Example
// tcp_server.c
#include
#include
#include
#include
#include
#define PORT 8080
#define BUFFER_SIZE 1024
int main()
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = 0;
char *hello = \\"Hello from server\\";
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
perror(\\"socket failed\\");
exit(EXIT_FAILURE);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0)
perror(\\"bind failed\\");
exit(EXIT_FAILURE);
if (listen(server_fd, 3) < 0)
perror(\\"listen\\");
exit(EXIT_FAILURE);
printf(\\"Waiting for a connection...\\\
\\");
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
perror(\\"accept\\");
exit(EXIT_FAILURE);
read(new_socket, buffer, BUFFER_SIZE);
printf(\\"Message from client: %s\\\
\\", buffer);
send(new_socket, hello, strlen(hello), 0);
printf(\\"Hello message sent\\\
\\");
close(new_socket);
close(server_fd);
return 0;
TCP Client
The client creates a socket, connects to the server, sends a message, and waits for a response.Client Code Example
// tcp_client.c
#include
#include
#include
#include
#include
#define PORT 8080
#define BUFFER_SIZE 1024
int main()
int sock = 0;
struct sockaddr_in serv_addr;
char *hello = \\"Hello from client\\";
char buffer[BUFFER_SIZE] = 0;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
printf(\\"Socket creation error\\\
\\");
return -1;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if(inet_pton(AF_INET, \\"127.0.0.1\\", &serv_addr.sin_addr)<=0)
printf(\\"Invalid address / Address not supported\\\
\\");
return -1;
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
printf(\\"Connection Failed\\\
\\");
return -1;
send(sock, hello, strlen(hello), 0);
printf(\\"Hello message sent\\\
\\");
int valread = read(sock, buffer, BUFFER_SIZE);
printf(\\"Message from server: %s\\\
\\", buffer);
close(sock);
return 0;
Compile and Run
Open two terminals. In the first, compile and run the server:gcc tcp_server.c -o tcp_server
./tcp_server
In the second, compile and run the client:
gcc tcp_client.c -o tcp_client
./tcp_client
Expected Output
Server terminal:Waiting for a connection...
Message from client: Hello from client
Hello message sent
Client terminal:
Hello message sent
Message from server: Hello from server
Conclusion
With socket programming, your C programs can communicate over networks, enabling powerful distributed applications.Subscribe to Our YouTube for More
https://blog.arashtad.com/updates/socket-programming-in-c-tcp-client-and-server/?feed_id=13644&_unique_id=6919fe452a341
Comments
Post a Comment