Client Server Model
The client-server model is a fundamental concept in networked applications, especially when programming in C. In this model, two distinct roles exist: the server and the client. The server is a program that waits for incoming requests from other computers or programs, typically listening on a specific port for connections. The client, on the other hand, initiates communication by connecting to the server, sending requests, and often waiting for responses.
The client is the program or device that initiates communication in the client-server model. You use the client to connect to a server, send requests, and wait for responses. Typically, the client knows the server's address and port in advance. In C networking, the client creates a socket, specifies the server's address, and uses the connect function to establish a connection.
The server is the program or device that waits for incoming connections from clients. It binds to a specific port and listens for requests using a socket. The server does not initiate communication; instead, it remains ready to accept and respond to clients at any time. In C, the server uses functions like bind, listen, and accept to manage incoming connections and serve multiple clients efficiently.
server.c
client.c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #define PORT 8080 int main() { int server_fd, new_socket; struct sockaddr_in address; int addrlen = sizeof(address); // Create socket file descriptor server_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_fd == 0) { perror("socket failed"); exit(EXIT_FAILURE); } // Bind socket to the port 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); } // Listen for incoming connections if (listen(server_fd, 3) < 0) { perror("listen"); exit(EXIT_FAILURE); } printf("Server is listening on port %d...\n", PORT); // Accept a connection new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen); if (new_socket < 0) { perror("accept"); exit(EXIT_FAILURE); } printf("Connection accepted!\n"); close(new_socket); close(server_fd); return 0; }
This separation of roles allows multiple clients to interact with a single server, making it possible to design scalable and flexible networked applications. The server is always ready to respond, while the client decides when to start the interaction. Both the server and client use sockets to communicate, but their roles in the process are clearly defined: the server listens and responds, while the client connects and requests.
The client-server model underpins most modern systems, enabling scalable and maintainable applications by separating client and server roles.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how sockets work in C?
What are some common use cases for the client-server model?
Can you provide a simple example of a client-server interaction in C?
Awesome!
Completion rate improved to 8.33
Client Server Model
Swipe to show menu
The client-server model is a fundamental concept in networked applications, especially when programming in C. In this model, two distinct roles exist: the server and the client. The server is a program that waits for incoming requests from other computers or programs, typically listening on a specific port for connections. The client, on the other hand, initiates communication by connecting to the server, sending requests, and often waiting for responses.
The client is the program or device that initiates communication in the client-server model. You use the client to connect to a server, send requests, and wait for responses. Typically, the client knows the server's address and port in advance. In C networking, the client creates a socket, specifies the server's address, and uses the connect function to establish a connection.
The server is the program or device that waits for incoming connections from clients. It binds to a specific port and listens for requests using a socket. The server does not initiate communication; instead, it remains ready to accept and respond to clients at any time. In C, the server uses functions like bind, listen, and accept to manage incoming connections and serve multiple clients efficiently.
server.c
client.c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #define PORT 8080 int main() { int server_fd, new_socket; struct sockaddr_in address; int addrlen = sizeof(address); // Create socket file descriptor server_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_fd == 0) { perror("socket failed"); exit(EXIT_FAILURE); } // Bind socket to the port 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); } // Listen for incoming connections if (listen(server_fd, 3) < 0) { perror("listen"); exit(EXIT_FAILURE); } printf("Server is listening on port %d...\n", PORT); // Accept a connection new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen); if (new_socket < 0) { perror("accept"); exit(EXIT_FAILURE); } printf("Connection accepted!\n"); close(new_socket); close(server_fd); return 0; }
This separation of roles allows multiple clients to interact with a single server, making it possible to design scalable and flexible networked applications. The server is always ready to respond, while the client decides when to start the interaction. Both the server and client use sockets to communicate, but their roles in the process are clearly defined: the server listens and responds, while the client connects and requests.
The client-server model underpins most modern systems, enabling scalable and maintainable applications by separating client and server roles.
Thanks for your feedback!