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.
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:...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
The Programming Language is C++ PLEASE, Make sure to read the requirements and grading criteria for...
The Programming Language is C++ PLEASE, Make sure to read the requirements and grading criteria for homework first... Thank you!!! Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three...
Objectives: 1. To get familiar with C# programming language 2. To get familiar with Visual Studio...
Objectives: 1. To get familiar with C# programming language 2. To get familiar with Visual Studio development environment 3. To practice on writing a C# program Task 1: Create documentation for the following program which includes the following: a. Software Requirement Specification (SRS) b. Use Case Task 2: Write a syntactically and semantically correct C# program that models telephones. Your program has to be a C# Console Application. You will not implement classes in this program other than the class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT