Question

In: Computer Science

Instructions: 1. Please use only C as the language of programming. 2. Please submit the following:...

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.

Solutions

Expert Solution

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\n");
scanf("%d",&port);
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
printf("Cant create\n");
else
printf("Socket is created\n");
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\n");
else
printf("Binded\n");
listen(sd,5);
clilen=sizeof(cliaddr);
newsd=accept(sd,(struct sockaddr *)&cliaddr,&clilen);
if(newsd<0)
{
printf("Cant accept\n");
}
else
printf("Accepted\n");
n=recv(newsd,rcv,100,0);
rcv[n]='\0';
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\n");
}
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....\n");

exit(0);

}

else

printf("Socket is created\n");

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\n");

else

printf("connected\n");

printf("Enter the existing file name\t");

scanf("%s",name);

printf("Enter the new file name\t");

scanf("%s",fname);

fp=fopen(fname,"w");

send(csd,name,sizeof(name),0);

while(1)

{

s=recv(csd,rcvg,100,0);

rcvg[s]='\0';

if(strcmp(rcvg,"error")==0)

printf("File is not available\n");

if(strcmp(rcvg,"completed")==0)

{

printf("File is transferred........\n");

fclose(fp);

close(csd);

break;

}

else

fputs(rcvg,stdout);

fprintf(fp,"%s",rcvg);

}

}


Related Solutions

Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
Please use the Java Programming language. This is the introductory course, chapter two. Please only use...
Please use the Java Programming language. This is the introductory course, chapter two. Please only use if/else if, else and while loop. We have not touch base with do and while do(I don't know if while do exist in Java). Create an application that converts number grades to letter grades. Console Welcome to the Letter Grade Converter Enter numerical grade: 90 Letter grade: A Continue? (y/n): y Enter numerical grade: 88 Letter grade: A Continue? (y/n): y Enter numerical grade:...
Be sure to use only C for the Programming Language in this problem. Before we start...
Be sure to use only C for the Programming Language in this problem. Before we start this, it is imperative that you understand the words “define”, “declare” and “initialize” in context of programming. It's going to help you a lot when following the guidelines below. Let's begin! Define two different structures at the top of your program. be sure to define each structure with exactly three members (each member has to be a different datatype). You may set them up...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The program will validate the characters in the array to see if they meets a set of requirements. Requirements: Must be at least 6 characters long (but not limited to 6, can be greater) Contains one upper case letter Contains one lower case letter Contains one digit Complete the code attached. Must use pointer while checking the characters. Download Source Lab 3 File: #include using...
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the...
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the following: 1) Your .cpp file of the solution. 2) For what n value (approximately) does your computer stop producing output? Why is this? Enter your answer in the 'Comments' field when you submit the file.   So far in our study of recursion we identified a few common recursive number sequences, such as the Fibonacci numbers. Number sequences like this are not recursive algorithms themselves...
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+)...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+) AND extend the code so that allocated memory is freed properly. Thank you struct foo { int a; char b; }; int main(void) { struct foo* arr[5]; int x; for(x = 0; x < 5; x++) { arr[x] = malloc(sizeof(struct foo)); arr[x]->a = 0; arr[x]->b = 'b'; } }
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function...
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function func2 has three parameters of type int, int, and double, say a, b, and c, respectively. Write the definition of func2 so that its action is as follows: Prompt the user to input two integers and store the numbers in a and b, respectively. If both of the numbers are nonzero: If a >= b, the value assigned to c is a to the...
Write a DFA simulator using the C++ programming language. Please refer to the following UDACITY website...
Write a DFA simulator using the C++ programming language. Please refer to the following UDACITY website if you do knot know C++. This is a free online tutorial for learning C++. Read your DFA from a textfile. First line contains a list of the final states (as integers), separated by a space. Rest of file contains the transitions in form: startstate, blank, symbol read, blank, tostate Prompt user for name of file. From there, program prompts the user for strings...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT