21bce1068 Lab1 Crypto

You might also like

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

CRYPTOGRAPHY AND NETWORK SECURITY

LAB 1

NAME : SNEHASIS GHOSH

REG NO. : 21BCE1068

CLIENT SERVER SOCKET PROGRAMMING TO TRANSFER FILE USING TCP


PROTOCOL

CLIENT

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

#define PORT 12345


#define MAX_BUFFER_SIZE 1024

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

// Create socket
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket < 0) {
perror("Error in socket creation");
exit(1);
}

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

// Connect to server
if (connect(clientSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
perror("Error in connecting to server");
exit(1);
}

printf("Connected to server on port %d\n", PORT);


// Receive file content from server
FILE *fileptr = fopen("received_file.txt", "wb");
if (fileptr == NULL) {
perror("Error in opening file");
exit(1);
}

while (1) {
ssize_t bytesRead = recv(clientSocket, buffer, sizeof(buffer), 0);
if (bytesRead < 0) {
perror("Error in receiving file");
exit(1);
}
if (bytesRead == 0)
break;

// Write received content to file


fwrite(buffer, 1, bytesRead, fileptr);

// Check if client or server wants to end the chat


if (strncmp(buffer, "bye", 3) == 0) {
break;
}

// Close file and socket


fclose(fileptr);
FILE *ptr = fopen("received_file.txt", "r");
char c= fgetc(ptr);
while(c!=EOF){
printf("%c",c);
c= fgetc(ptr);
}
fclose(ptr);
close(clientSocket);

printf("File received. Connection closed.\n");

return 0;
}

SERVER

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

#define PORT 12345


#define MAX_BUFFER_SIZE 1024

int main() {
int serverSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
socklen_t addrSize;
FILE *fileptr;
char buffer[MAX_BUFFER_SIZE];

// Create socket
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket < 0) {
perror("Error in socket creation");
exit(1);
}

serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);

// Bind socket to IP and port


if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
perror("Error in binding");
exit(1);
}

// Listen for incoming connections


if (listen(serverSocket, 5) < 0) {
perror("Error in listening");
exit(1);
}

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

addrSize = sizeof(clientAddr);
// Accept connection from client
clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr, &addrSize);
if (clientSocket < 0) {
perror("Error in accepting connection");
exit(1);
}

printf("Client connected\n");

// Open the file to send


fileptr = fopen("file.txt", "rb");
if (fileptr == NULL) {
perror("Error in opening file");
exit(1);
}

// Read and send the file content


while (!feof(fileptr)) {
size_t bytesRead = fread(buffer, 1, sizeof(buffer), fileptr);
if (bytesRead > 0) {
// Send file content to client
if (send(clientSocket, buffer, bytesRead, 0) == -1) {
perror("Error in sending file");
exit(1);
}
}
}

// Close file and sockets


fclose(fileptr);
close(clientSocket);
close(serverSocket);

printf("File sent successfully. Connection closed.\n");

return 0;
}

OUTPUT

You might also like