Concurrent Code Explanation
Concurrent Code Explanation
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <windows.h>
• This line tells the compiler to link against the ws2_32.lib, which is the library needed for
using Winsock functions.
3. Function Declarations
• Declares a function handle_client that takes a SOCKET type parameter, which will be
used to communicate with a connected client.
handle_client(client_socket);
return 0;
• DWORD WINAPI ClientThread(LPVOID lpParam): This defines a thread function that will
handle client connections.
o Parameters:
▪ LPVOID lpParam: A generic pointer that can hold any type of data. Here,
it is expected to be a SOCKET.
o The function casts lpParam back to a SOCKET and calls handle_client to process
the client.
5. Main Function
int main() {
6. Winsock Initialization
WSADATA wsa;
7. Create a Socket
• Declares two socket variables: server_socket for listening for incoming connections and
client_socket for communication with a client.
• Creates a socket:
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(3001);
o sin_port: Port number (3001), converted to network byte order using htons().
listen(server_socket, 3);
• Puts the server socket in a listening state, allowing it to accept incoming connections.
The second parameter (3) is the maximum length of the queue of pending connections.
while (1) {
• accept() blocks until a client connects, returning a new socket (client_socket) for
communication with that client.
closesocket(server_socket);
WSACleanup();
return 0;
• Calculates the sum of the two integers and sends the result back to the client.
closesocket(client_socket);
Summary
This server program uses a multithreaded approach to handle multiple client connections. It
initializes Winsock, creates a listening socket, accepts incoming connections in a loop, and
creates a new thread for each client to handle their requests independently. The
communication is done by sending and receiving integers, specifically summing two integers
sent by the client.