0% found this document useful (0 votes)
4 views6 pages

Project 5

The document contains C code for a client-server application where the client registers a file with its metadata (filename, hash, and port) to the server. The server maintains a linked list of registered files and their associated clients, handling incoming connections and displaying registered files. It includes functions for socket creation, connection handling, and basic JSON parsing for file registration.

Uploaded by

drali2662
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Project 5

The document contains C code for a client-server application where the client registers a file with its metadata (filename, hash, and port) to the server. The server maintains a linked list of registered files and their associated clients, handling incoming connections and displaying registered files. It includes functions for socket creation, connection handling, and basic JSON parsing for file registration.

Uploaded by

drali2662
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PROJECT 5

Client code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SERVER_IP "127.0.0.1"


#define SERVER_PORT 8080
#define BUFFER_SIZE 1024

int main() {
int clientSocket;
struct sockaddr_in serverAddr;
char buffer[BUFFER_SIZE];

clientSocket = socket(AF_INET, SOCK_STREAM, 0);


if (clientSocket == -1) {
perror("Socket creation failed");
return EXIT_FAILURE;
}

serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, SERVER_IP, &serverAddr.sin_addr);

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


sizeof(serverAddr)) < 0) {
perror("Connection failed");
return EXIT_FAILURE;
}

// Sample file metadata (filename, hash, and client port)


char filename[] = "example.txt";
char fullFileHash[] =
"abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890";
int clientPort = 5001;

// Creating JSON string manually


snprintf(buffer, BUFFER_SIZE,
"{\"filename\":\"%s\",\"fullFileHash\":\"%s\",\"clientPort\":%d}",
filename, fullFileHash, clientPort);

// Send data
send(clientSocket, buffer, strlen(buffer), 0);
printf("File registration sent to server.\n");

close(clientSocket);
return 0;
}

Server Code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>

#define PORT 8080


#define MAXPEERS 10
#define BUFFER_SIZE 1024

// Structure to store file info and clients


struct FileInfo {
char filename[100];
char fullFileHash[65]; // SHA-256 hash (64 hex chars + null
terminator)
char clientIP[MAXPEERS][INET_ADDRSTRLEN];
int clientPort[MAXPEERS];
int numberOfPeers;
struct FileInfo *next;
};

// Head of linked list


struct FileInfo *head = NULL;

// Function to find a file entry by hash


struct FileInfo* findFileByHash(const char *hash) {
struct FileInfo *current = head;
while (current != NULL) {
if (strcmp(current->fullFileHash, hash) == 0) return current;
current = current->next;
}
return NULL;
}

// Function to check if client is already registered


int isClientRegistered(struct FileInfo *file, const char *ip, int port) {
for (int i = 0; i < file->numberOfPeers; i++) {
if (strcmp(file->clientIP[i], ip) == 0 && file->clientPort[i] ==
port) {
return 1; // Already registered
}
}
return 0;
}

// Function to register file


void registerFile(const char *filename, const char *hash, const char *ip,
int port) {
struct FileInfo *file = findFileByHash(hash);

if (file != NULL) {
// File exists, add client if not already registered
if (!isClientRegistered(file, ip, port)) {
strcpy(file->clientIP[file->numberOfPeers], ip);
file->clientPort[file->numberOfPeers] = port;
file->numberOfPeers++;
}
} else {
// Create new file entry
struct FileInfo *newFile = (struct FileInfo
*)malloc(sizeof(struct FileInfo));
strcpy(newFile->filename, filename);
strcpy(newFile->fullFileHash, hash);
strcpy(newFile->clientIP[0], ip);
newFile->clientPort[0] = port;
newFile->numberOfPeers = 1;
newFile->next = head;
head = newFile;
}
}

// Function to display registered files


void displayFiles() {
struct FileInfo *current = head;
printf("\n=== Registered Files ===\n");
while (current != NULL) {
printf("File: %s\nHash: %s\nClients:\n", current->filename,
current->fullFileHash);
for (int i = 0; i < current->numberOfPeers; i++) {
printf(" - %s:%d\n", current->clientIP[i], current-
>clientPort[i]);
}
printf("-------------------------\n");
current = current->next;
}
}

// Function to handle client connections


void handleClient(int clientSocket, struct sockaddr_in clientAddr) {
char buffer[BUFFER_SIZE] = {0};
recv(clientSocket, buffer, BUFFER_SIZE, 0);

char filename[100], hash[65];


int clientPort;

// Basic parsing (not full JSON parsing)


sscanf(buffer, "{\"filename\":\"%[^\"]\",\"fullFileHash\":\"%
[^\"]\",\"clientPort\":%d}", filename, hash, &clientPort);

char clientIP[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &clientAddr.sin_addr, clientIP, INET_ADDRSTRLEN);

registerFile(filename, hash, clientIP, clientPort);


displayFiles();

close(clientSocket);
}

// Main server function


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

serverSocket = socket(AF_INET, SOCK_STREAM, 0);


if (serverSocket == -1) {
perror("Socket creation failed");
return EXIT_FAILURE;
}

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)) < 0) {
perror("Bind failed");
return EXIT_FAILURE;
}

if (listen(serverSocket, 10) < 0) {


perror("Listen failed");
return EXIT_FAILURE;
}

printf("Server is listening on port %d...\n", PORT);

while (1) {
clientSocket = accept(serverSocket, (struct sockaddr
*)&clientAddr, &addrSize);
if (clientSocket < 0) {
perror("Accept failed");
continue;
}
handleClient(clientSocket, clientAddr);
}

close(serverSocket);
return 0;
}
Output :

You might also like