AIM: Implementation of File Transfer using sockets
THEORY:
File transfer using sockets is a classic use-case of TCP-based client-server communication. In this experiment, a file is read on the server side and transmitted over a TCP connection to the client, which receives the data and writes it into a new file.
Key Concepts:
Socket Programming: This enables communication between two machines using IP and port-based addressing.
TCP (Transmission Control Protocol): A reliable, connection-oriented protocol used to ensure that all data packets arrive correctly and in order.
Server: Waits for client connection, reads a file, and sends its content over the socket.
Client: Connects to the server and receives the file content, storing it locally.
The server creates a socket and waits for incoming connections. Upon a client's request, it opens a file (test.txt), reads the contents in chunks, and sends them over the socket. The client receives this data and writes it into a new file (temp.txt).
CODE:
FTP SERVER:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define CHUNK 1024 // read 1024 bytes at a time
void readfile(int new_sd) {
char buf[CHUNK];
int n;
FILE *file;
file = fopen("test.txt", "r");
if(file == NULL) {
printf("\n Error in opening the file");
} else {
while(fgets(buf, sizeof(buf), file) != NULL) {
n = strlen(buf);
write(new_sd, buf, n);
bzero(buf, sizeof(buf));
}
fclose(file);
}
}
int main(int argc, char **argv) {
int sd, new_sd, client_len, port, n, yes = 1;
struct sockaddr_in server, client;
char buf[1024];
port = atoi(argv[1]);
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "can't create a socket\n");
exit(1);
}
server.sin_family = AF_INET;
server.sin_port = port;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
fprintf(stderr, "can't bind name to socket\n");
exit(1);
}
listen(sd, 5);
while(1) {
client_len = sizeof(client);
if ((new_sd = accept(sd, (struct sockaddr *)&client, &client_len)) == -1) {
fprintf(stderr, "can't accept client\n");
exit(1);
}
n = read(new_sd, buf, sizeof(buf));
readfile(new_sd);
close(new_sd);
}
close(sd);
return 0;
}
FTP CLIENT:
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define CHUNK 1024
void writefile(int sd) {
char buf[CHUNK];
FILE *file;
file = fopen("temp.txt", "w");
if(file == NULL) {
printf("\n Error in opening the file");
return;
}
while(read(sd, buf, sizeof(buf)) > 0) {
fputs(buf, file);
bzero(buf, sizeof(buf));
}
fclose(file);
}
int main(int argc, char **argv) {
int sd, port;
char buf[1024];
struct sockaddr_in server;
port = atoi(argv[1]);
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "can't create a socket\n");
exit(1);
}
server.sin_family = AF_INET;
server.sin_port = port;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
fprintf(stderr, "can't connect\n");
exit(1);
}
printf("\nEnter the command: ");
scanf("%s", buf); // get command text (can be anything, not used)
write(sd, buf, sizeof(buf));
writefile(sd);
close(sd);
return 0;
}
OUTPUT:
CONCLUSION:
This experiment demonstrated the use of TCP sockets in C for transferring files between a server and client. By successfully setting up the file transfer system, it showcases the ability to build reliable communication between devices over a network. It also reinforces understanding of socket creation, binding, listening, accepting connections, and data transmission. The practical exposure to socket programming is crucial for systems and network programming tasks.