Instructions: 1. Please use only C as the language of programming. 2. Please sub
ID: 3800612 • Letter: I
Question
Instructions:
1. Please use only C as the language of programming.
2. Please submit the following: (1) the client and the server source files each (2) a brief Readme le that shows the usage of the program.
3. Please appropriately comment your program and name all the identifiers suitable, to enable enhanced readability of the code.
Problem:
Write an ftp client and an ftp server such that the client sends a request to ftp server for downloading a file. The server responds
with the contents of the file which is then stored on the client's local directory. The server checks the size of
the file before responding and if the file size is greater than 1024 bytes, instead of sending the file, the server
responds with the message "File size greater than 1 KB" and waits further for another request from the
client. The connection is terminated only when client terminates the same.
Explanation / Answer
Server
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
int main()
{
FILE *fp;
int sd,newsd,ser,n,a,cli,pid,bd,port,clilen;
char name[100],fileread[100],fname[100],ch,file[100],rcv[100];
struct sockaddr_in servaddr,cliaddr;
printf("Enter the port address ");
scanf("%d",&port);
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
printf("Cant create ");
else
printf("Socket is created ");
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(port);
a=sizeof(servaddr);
bd=bind(sd,(struct sockaddr *)&servaddr,a);
if(bd<0)
printf("Cant bind ");
else
printf("Binded ");
listen(sd,5);
clilen=sizeof(cliaddr);
newsd=accept(sd,(struct sockaddr *)&cliaddr,&clilen);
if(newsd<0)
{
printf("Cant accept ");
}
else
printf("Accepted ");
n=recv(newsd,rcv,100,0);
rcv[n]='';
fp=fopen(rcv,"r");
if(fp==NULL)
{
send(newsd,"error",5,0);
close(newsd);
}
else
{
while(fgets(fileread,sizeof(fileread),fp))
{
if(send(newsd,fileread,sizeof(fileread),0)<0 || send(newsd,fileread,sizeof(fileread),0)>1024)
{
printf("Cant send ");
}
sleep(1);
}
if(!fgets(fileread,si zeof(fileread),fp))
{
send(newsd,"completed",999999999,0);
}
return(0);
}
}
Client
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main()
{
FILE *fp;
int csd,n,ser,s,cli,cport,newsd;
char name[100],rcvmsg[100],rcvg[100],fname[100];
struct sockaddr_in servaddr;
printf("Enter the port");
scanf("%d",&cport);
csd=socket(AF_INET,SOCK_STREAM,0);
if(csd<0)
{
printf("Error.... ");
exit(0);
}
else
printf("Socket is created ");
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(cport);
if(connect(csd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
printf("Error in connection ");
else
printf("connected ");
printf("Enter the existing file name ");
scanf("%s",name);
printf("Enter the new file name ");
scanf("%s",fname);
fp=fopen(fname,"w");
send(csd,name,sizeof(name),0);
while(1)
{
s=recv(csd,rcvg,100,0);
rcvg[s]='';
if(strcmp(rcvg,"error")==0)
printf("File is not available ");
if(strcmp(rcvg,"completed")==0)
{
printf("File is transferred........ ");
fclose(fp);
close(csd);
break;
}
else
fputs(rcvg,stdout);
fprintf(fp,"%s",rcvg);
}
}