CS22B2043 Lab6

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

Lab 6 Assignment

CS22B2043

B.RITESH KUMAR

Ǫn1) Assignment 6: To develop a client-server file transfer


system using TCP sockets in C where the client can request a
file from the server, and the server will send the requested file
to the client and vice versa.

• Create a TCP server that listens for incoming


connections from clients.
• Create a file using the program The server waits for a file
request (file name) from the client.
• The server sends the requested file to the client.
• If the file does not exist, it sends an appropriate error
message.
• Create a TCP client that connects to the server.
• The client sends the name of the file it wants to download
from the server.
• The client receives the file and saves it locally.
• Implementation to read the received file's content and
display it after the file has been successfully transferred.
• TCP sockets with an acknowledgement/s (ACK).
Server code

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

#define PORT 8080


#define BUFFER_SIZE 1024

// Function to send a file to the client


void send_file_to_client(int new_socket, char *filename) {
FILE *fp = fopen(filename, "r");

if (fp == NULL) {
printf("File not found. Creating a new file...\n");
fp = fopen(filename, "w");
if (fp == NULL) {
perror("Error creating new file");
exit(1);
}
fprintf(fp, "This is a newly created file that is being sent to the
client.\n");
fclose(fp);
fp = fopen(filename, "r");
}

char data[BUFFER_SIZE] = {0};


while (fgets(data, BUFFER_SIZE, fp) != NULL) {
if (send(new_socket, data, sizeof(data), 0) == -1) {
perror("Error sending file to client.");
exit(1);
}
memset(data, 0, BUFFER_SIZE);
}

printf("File %s sent to client successfully\n", filename);


fclose(fp);
}

// Function to receive a file from the client


void receive_file_from_client(int new_socket, char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
perror("Error opening file to receive");
exit(1);
}

char buffer[BUFFER_SIZE] = {0};


int bytes_received;
printf("Receiving file from client and saving as %s:\n", filename);

while ((bytes_received = recv(new_socket, buffer, BUFFER_SIZE, 0)) > 0) {


fprintf(fp, "%s", buffer);
printf("%s", buffer); // Print the contents being received
memset(buffer, 0, BUFFER_SIZE);
}

printf("\nFile received and saved as %s\n", filename);


fclose(fp);
}

int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
char filename[BUFFER_SIZE];
char operation[10];

// Creating socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);

// Bind the socket to the network


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 failed");
exit(EXIT_FAILURE);
}

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

// Accept client connections


if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen)) < 0) {
perror("Accept failed");
exit(EXIT_FAILURE);
}

// Receive operation (UPLOAD or REǪUEST)


recv(new_socket, operation, sizeof(operation), 0);

if (strcmp(operation, "REǪUEST") == 0) {
// Receive file request from the client
recv(new_socket, buffer, BUFFER_SIZE, 0);
printf("Client requested file: %s\n", buffer);
strcpy(filename, buffer);

// Send the file to the client


send_file_to_client(new_socket, filename);

} else if (strcmp(operation, "UPLOAD") == 0) {


// Receive file from the client
recv(new_socket, buffer, BUFFER_SIZE, 0);
printf("Client is uploading file: %s\n", buffer);
strcpy(filename, buffer);

// Receive the file


receive_file_from_client(new_socket, filename);
}

close(new_socket);
close(server_fd);
return 0;
}
Client code

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

#define PORT 8080


#define BUFFER_SIZE 1024

// Function to send a file to the server


void send_file_to_server(int sockfd, char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
perror("Error opening file to send");
exit(1);
}

char data[BUFFER_SIZE] = {0};


while (fgets(data, BUFFER_SIZE, fp) != NULL) {
if (send(sockfd, data, sizeof(data), 0) == -1) {
perror("Error sending file to server.");
exit(1);
}
memset(data, 0, BUFFER_SIZE);
}

printf("File %s sent to server successfully\n", filename);


fclose(fp);
}

// Function to receive a file from the server and display its content
void receive_file(int sockfd, char *filename) {
int n;
FILE *fp;
char buffer[BUFFER_SIZE];

fp = fopen(filename, "w");
if (fp == NULL) {
perror("Error opening file");
exit(1);
}

printf("\nReceiving file and displaying contents:\n");

while ((n = recv(sockfd, buffer, BUFFER_SIZE, 0)) > 0) {


fprintf(fp, "%s", buffer);
printf("%s", buffer);
memset(buffer, 0, BUFFER_SIZE);
}
printf("\nFile received and saved as %s\n", filename);
fclose(fp);
}

int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char filename[BUFFER_SIZE];
int choice;

// Creating socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation error");
exit(EXIT_FAILURE);
}

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {


perror("Invalid address / Address not supported");
exit(EXIT_FAILURE);
}

// Connect to the server


if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Connection failed");
exit(EXIT_FAILURE);
}

printf("1. Request a file from the server\n");


printf("2. Send a file to the server\n");
printf("Enter your choice: ");

// Check return value of scanf


if (scanf("%d", &choice) != 1) {
fprintf(stderr, "Error reading choice\n");
exit(EXIT_FAILURE);
}

if (choice == 1) {
// Request a file from the server
printf("Enter the filename to request from the server: ");
if (scanf("%s", filename) != 1) {
fprintf(stderr, "Error reading filename\n");
exit(EXIT_FAILURE);
}
send(sock, "REǪUEST", sizeof("REǪUEST"), 0); // Notify server for
download
send(sock, filename, sizeof(filename), 0); // Send filename to the
server

// Receive the file


receive_file(sock, filename);

} else if (choice == 2) {
// Send a file to the server
printf("Enter the filename to send to the server: ");
if (scanf("%s", filename) != 1) {
fprintf(stderr, "Error reading filename\n");
exit(EXIT_FAILURE);
}
send(sock, "UPLOAD", sizeof("UPLOAD"), 0); // Notify server for upload
send(sock, filename, sizeof(filename), 0); // Send filename to the
server

// Send the file


send_file_to_server(sock, filename);
}

close(sock);
return 0;
}
Server to client

Output at server

Output at client
Client to server

Server output

Client output

You might also like