p23cs025 Lab3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Department of Computer Science and Engineering, SVNIT Surat

Subject: Distributed Systems


Assignment 3

Roll No.: p23cs026

Implement given scenario for the Client Server Programming.


1. Come up with the Client-Server massage passing chat application. Client and Server
should be able
to send the message to each other until one of them quit or terminate.
2. Using Client-Server communication mechanism get the load status of other nodes in your
network
(identify the states of other nodes in the system – Overload, Moderate, lightly loaded).
Implement
Client-Server model. Run the client and server instance on same machine and pass the
message from
client to server or server to client. Get the CPU load of client or server and state that either it
is
under loaded or overloaded on the other one.
The client server communication mechanism has the limitation that it only handles one
connection
at a time and then terminates. A real-world server should run indefinitely and should have
the
capability of handling a number of simultaneous connections, each in its own process.
Code:
Step 1: server.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>

#define PORT 12345


#define BUFFER_SIZE 128

int main() {
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
int addrSize = sizeof(struct sockaddr_in);

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {


perror("Error initializing WinSock");
return EXIT_FAILURE;
}

serverSocket = socket(AF_INET, SOCK_STREAM, 0);


if (serverSocket == INVALID_SOCKET) {
perror("Error creating socket");
WSACleanup();
return EXIT_FAILURE;
}

memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
if (bind(serverSocket, (struct sockaddr*)&serverAddr,
sizeof(serverAddr)) == SOCKET_ERROR) {
perror("Error binding socket");
closesocket(serverSocket);
WSACleanup();
return EXIT_FAILURE;
}

if (listen(serverSocket, 1) == SOCKET_ERROR) {
perror("Error listening for connections");
closesocket(serverSocket);
WSACleanup();
return EXIT_FAILURE;
}

clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr,


&addrSize);
if (clientSocket == INVALID_SOCKET) {
perror("Error accepting connection");
closesocket(serverSocket);
WSACleanup();
return EXIT_FAILURE;
}

char buffer[BUFFER_SIZE];
while (1) {
memset(buffer, 0, sizeof(buffer));

int bytesRead = recv(clientSocket, buffer, sizeof(buffer), 0);


if (bytesRead <= 0) {
perror("Error receiving message");
break;
}

printf("Received from client: %s\n", buffer);

FILE* fp = fopen("/proc/loadavg", "r");


if (fp != NULL) {
fscanf(fp, "%s", buffer);
double load = atof(buffer);
double loadPercentage = load * 100;
fclose(fp);

send(clientSocket, (char*)&loadPercentage, sizeof(double),


0);
} else {
perror("Error opening /proc/loadavg file");
}
}

closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();

return 0;
}

Step 2: client.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <winsock2.h>
#define PORT 12345

#define BUFFER_SIZE 128

int main() {

WSADATA wsaData;

SOCKET clientSocket;

struct sockaddr_in serverAddr;

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {

perror("Error initializing WinSock");

return EXIT_FAILURE;

clientSocket = socket(AF_INET, SOCK_STREAM, 0);

if (clientSocket == INVALID_SOCKET) {

perror("Error creating socket");

WSACleanup();

return EXIT_FAILURE;

memset(&serverAddr, 0, sizeof(serverAddr));

serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

serverAddr.sin_port = htons(PORT);

if (connect(clientSocket, (struct sockaddr*)&serverAddr,


sizeof(serverAddr)) == SOCKET_ERROR) {

perror("Error connecting to server");

closesocket(clientSocket);

WSACleanup();

return EXIT_FAILURE;

char message[BUFFER_SIZE];

while (1) {

memset(message, 0, sizeof(message));

printf("Enter message (or type 'quit' to exit): ");

fgets(message, sizeof(message), stdin);

int bytesSent = send(clientSocket, message, strlen(message),


0);

if (bytesSent <= 0) {

perror("Error sending message");

break;

}
if (strcmp(message, "quit\n") == 0) {

break;

double loadPercentage;

int bytesRead = recv(clientSocket, (char*)&loadPercentage,


sizeof(double), 0);

if (bytesRead <= 0) {

perror("Error receiving CPU load status");

break;

printf("Server CPU Load Status: %.2f%%\n", loadPercentage);

closesocket(clientSocket);

WSACleanup();

return 0;

}
Step 3: Compile & Run the server code

Step 4: Compile & Run the client code. Send messages


between client and server

You might also like