0% found this document useful (0 votes)
121 views7 pages

File Transfer Using TCP

This document describes a program that transfers a file from a server to a client using TCP socket programming. The server code creates a socket, binds it to a port, and listens for incoming connections. When a client connects, the server reads the requested file name from the client and sends the file contents. The client code connects to the server, sends the file name, receives the file contents and saves it locally under a new file name. The file transfer is successful and the key steps of both the server and client code are outlined.

Uploaded by

Pranav Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
121 views7 pages

File Transfer Using TCP

This document describes a program that transfers a file from a server to a client using TCP socket programming. The server code creates a socket, binds it to a port, and listens for incoming connections. When a client connects, the server reads the requested file name from the client and sends the file contents. The client code connects to the server, sends the file name, receives the file contents and saves it locally under a new file name. The file transfer is successful and the key steps of both the server and client code are outlined.

Uploaded by

Pranav Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

File Transfer using TCP

Assignment 3

Name : SB Pranav Raj


Rollno: 195001078
Section: CSE-B.
Aim:
Transfer a file from server to client using TCP Socket programming.
Program Code:
Server:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
int sockfd,len_c,newfd;
char str[1024];
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("\nError in socket creation!!");
return -1;
}
printf("\nSocket Created Successfully!!");
struct sockaddr_in server_addr,client_addr,clients_addr[5];
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=INADDR_ANY;
server_addr.sin_port=htons(8081);
if(bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr))<0)
{
perror("Bind error");
}
listen(sockfd,5);
printf("\nServer listening now...");
len_c=sizeof(client_addr);
int clients[5],client_len=0;
newfd=accept(sockfd,(struct sockaddr *)&client_addr,&len_c);
printf("\nServer accepted client");
int st=read(newfd,str,sizeof(str));
printf("\nThe file to be transfered to the client is: %s",str);
FILE *ptr;
ptr=fopen(str,"r");
if(ptr==NULL)
{
printf("\nFile couldn't be opened");
return 0;
}
char buff[100];
char fileContents[1024];
int line_no=0;
printf("\n");
while(1)
{
char c=fgetc(ptr);
if(feof(ptr))
break;
fileContents[line_no++]=c;
}
fileContents[line_no]='\0';
st=write(newfd,fileContents,sizeof(fileContents));
printf("\nFile Transfer Successful!!!");
close(newfd);
close(sockfd);

printf("\n");
return 0;
}

Client:
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr,client_addr;
char str[1024];
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("\nSocket not created");
return -1;
}
printf("\nSuccesful socket created");

bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
server_addr.sin_port=htons(8081);
connect(sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr));
printf("\nSuccesfully connected to the server!!");

printf("\nEnter file name : ");


scanf("%[^\n]s",str);
char fname[100];
strcpy(fname,str);
int n=write(sockfd,str,sizeof(str));
n=read(sockfd,str,sizeof(str));
char temp[1024];
printf("Enter the name in which the file is to be saved:\n");
scanf("%s",temp);
FILE *ptr=fopen(temp,"w+");
if(ptr==NULL)
{
printf("\nError while opening file");
return 0;
}
fputs(str,ptr);
fclose(ptr);
ptr=fopen(temp,"r");
printf("\nSaved in file: %s\n", temp);
printf("Contents of the file:\n");
while(1)
{
char c=fgetc(ptr);
if(feof(ptr))
break;
else
printf("%c",c);
}
fclose(ptr);
close(sockfd);
printf("\n");
return 0;
}

Sample Input/Output:
Result:
The requested file from the client was successfully transferred by the server
and a new file with the same contents was created successfully.

You might also like