0% found this document useful (0 votes)
22 views4 pages

File Transfer

Uploaded by

sathul257
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)
22 views4 pages

File Transfer

Uploaded by

sathul257
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/ 4

File Transfer Protocol

Client
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

int main(int argc, char *argv[]) {


int socket_desc;
char message[2000], filename[2000];
struct sockaddr_in server;
FILE *fp;

// Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
printf("Could not create socket");
return 1;
}

server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(8888);

// Connect to remote server


if (connect(socket_desc, (struct sockaddr*)&server, sizeof(server)) < 0) {
puts("connect error");
return 1;
}
puts("Connected");

strcpy(filename, "sample.txt");
printf("Filename is %s with %lu", filename, strlen(filename));

// Send filename to server


if (send(socket_desc, filename, strlen(filename), 0) < 0) {
puts("Send failed");
return 1;
}

// Open file for writing


fp = fopen("backup", "w");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}

// Receive file content from server and write to file


while (recv(socket_desc, message, 2000, 0)) {
if (strcmp(message, "EOF") == 0) {
// End of file reached
break;
} else {
// Write received data to file
fprintf(fp, "%s", message);
// Clear message buffer
memset(message, 0, sizeof(message));
}
}

// Close the file


fclose(fp);

close(socket_desc);
return 0;
}

Server

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[]) {


int socket_desc, new_socket, c;
char filename[2000], message[2000];
FILE *fp;
struct sockaddr_in server, client;

// Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
printf("Could not create socket");
}
printf("Socket created\n");
// Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);

// Bind
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("Bind failed");
return 1;
}
puts("Bind done");

// Listen
listen(socket_desc, 3);

// Accept and incoming connection


puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c);
if (new_socket < 0) {
perror("Accept failed");
return 1;
}
puts("Connection accepted");

// Receive filename from client


recv(new_socket, filename, 2000, 0);
filename[strlen(filename)] = '\0'; // Ensure null-termination
strcpy(filename, "sample.txt");
printf("Filename is %s with %lu", filename, strlen(filename));

// Open the file


fp = fopen(filename, "r");
if (fp == NULL) {
// If file not found, send error message to client
strcpy(message, "File Not Found\r\n");
printf("%s", message);
send(new_socket, message, strlen(message), 0);
}
else {
// If file found, send file contents to client
while (fgets(message, 2000, fp) != NULL) {
printf("%s", message);
send(new_socket, message, strlen(message), 0);
}
fclose(fp);
}

// Send EOF to indicate end of file transmission


strcpy(message, "EOF");
send(new_socket, message, strlen(message), 0);

// Close the socket


close(socket_desc);
return 0;
}

You might also like