Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Network & Security Expert

Goals:

1. To understand the challenges of protocol design.

2. To discover and appreciate the challenges of developing complex, real-world network applications.

3. Make sense of real-world sockets programming APIs.

4. To utilize a sockets programming API to construct simplified FTP server and client applications.

Overview

In this assignment, you will implement (simplified) FTP server and FTP client. The client shall connect to the server and support uploading and downloading of files to/from server. Before continuing, please read the Preliminaries section, below. It will save you hours of frustration.

Consider codes for client and server codes below.

1 # Server code
2 from socket import ∗
3
4 # The port on which to listen
5 server Por t = 12000
6
7 # Create a TCP s ocke t
8 server Socket = socket ( AF INET ,SOCK STREAM) 9
10 # Bind the socke t to the port
11 server Socket . bind ( ( ' ' , server Por t ) ) 12
13 # Start listening for incoming connections
14 server Socket. listen ( 1 )

15
16 print "The server is ready to receive " 17
18 # The buffer to store the rreceived data
19 data = ""
20
21 # Forever accept incoming connections
22 while 1 :
23 # Accept a connection ; get client ' ssocket
24 connection Socket , addr = server Socket . accept ( ) 25
26 # Receive whatever the newly connected client has to send
27 data = connection Socket . recv ( 40 ) 28
29 print data 30
31 # Close the s ocke t
32 connection Socket . c l o se ( )

1 # Client code
2 from socket import ∗
3
4 # Name and port number of the server to
5 # which want to connect .
6 serverName = " ecs . fullerton . edu"
7 server Por t = 12000 8
9 # Create as ocke t
10 client Socket = socket ( AF INET , SOCK STREAM) 11
12 # Connect to the server
13 client Socket . connect ( ( serverName , server Por t ) ) 14
15 # As tring we want to send to the server
16 data = " Hello world ! This is a very long string . " 17
18 # Send that string !
19 client Socket . send ( data )
20
21 # Close the socke t
22 client Socket . close ( )

In the code above, the client connects to the server and sends string "Hello world! This is a very long string.". Both codes are syntactically correct. Whenever the client connects to the server, we would expect the server to print out the 40 character string sent by the client. In practice, this may not always be the case. The potential problems are outlined below:

• Problem 1: send(data) is not guaranteed to send all bytes of the data: As we learned in class, each socket has an associated send buffer. The buffer stores application data ready to be sent off. Whenever you call the send(), behind the scenes, the operating system copies the data from the buffer in your program into the send buffer of the socket. Your operating system will then take the data in the send buffer, convert it into a TCP segment, and push the segment down the protocol stack. send(), however, will return immediately after the socket's network buffer has been filled. This means that in the client code above, if the data string was large, (i.e. larger than the network buffer), the send() would return before sending all bytes of data.

• Problem 2: data = connectionSocket.recv(40) is not guaranteed to receive all 40 bytes: This can happen even if the sender has sent all 40 bytes. Before delving into this problem, lets review the behavior of recv(). Functionrecv() will block until either
a) the other side has closed their socket, or b) some data has been received.

Case a) is generally not a problem. It can be easily detected by adding line if not data: immediately after data = connectionSocket.recv(40) in the receiver (which in the above example is the server). The test will evaluate to true if the other side has closed their socket.

In Case b), the variable data will contain the received data. However, data may not be 40 bytes in size (again, even if the sender sent 40 bytes). The reasons can be as follows:

1. Not all data may arrive at the same time: Recall that Internet is a packet switched network. Big chunks of data are split into multiple packets. Some of these packets may arrive at the receiver faster than others. Hence, it is possible that 20 bytes of the string arrive at the receiver, while 20 more are still on their way. recv(), however, will return as soon as it gets the first 20 bytes.

2. recv() returns after emptying the receive buffer of the socket: as we learned in class, each socket also has a receive buffer. That buffer stores all arriving data that is ready to be retrieved by the application. recv() returns after emptying the receive buffer. Because the receive buffer may not contain all data sent by the sender when the recv() is called, recv() will return before having received the specified number of bytes.

So how do we cope with the above problems? The answer is that it is up to you, the application developer, to ensure that all bytes are sent and received. This can be done by calling send() and recv() inside the loop, until all data has been sent/received. First, lets fix our client to ensure that it sends all of the specified bytes:

1 # Clie nt code
2 from s o c k e t import ∗
3
4 # Name and port number of the serve r to
5 # which want to connect .

6 serverName = e c s . f u l l e r t o n . e d u
7 s e rv e r Po r t = 12000
8
9 # Create a s ocke t
10 c l i e n t S o c k e t = s o c k e t ( AF INET , SOCK STREAM) 11
12 # Connect to the serve r
13 c l i e n t S o c k e t . connect ( ( serverName , s e rv e r Po r t ) ) 14
15 # A s tring we want to send to the serve r
16 data = " Hel l o world! This i s a very long s t r i n g . "
17
18 # b y te s Se nt = 0
19
20 # Keep sending b y te s until a l l b y te s are sent
21 while bytes Sent != len ( data ) :
22 # Send that s tring !
23 bytes Sent += c l i e n t S o c k e t . send ( data [ bytes Sent : ] ) 24
25 # Close the s ocke t
26 c l i e n t S o c k e t . c l o s e ( )

We made three changes. First, we added an integer variable, bytesSent, which keeps track of how many bytes the client has sent. We then added line: while bytesSent != len(data):. This loop will repeatedly call send() until bytesSent is equal to the length of our data. Another words, this line says "keep sending until all bytes of data have been sent." Finally, inside the loop, we have line bytesSent += clientSocket.send(data[bytesSent:]). Recall, that send() returns the number of bytes it has just sent. Hence, whenever send() sends x bytes, bytesSent will be incremented by x. The parameter, data[bytesSent:], will return all bytes that come after the first bytesSent bytes of data. This ensures that in the next iteration of the loop, we will resume sending at the offset ofdatawhere we left off.
With the client code working, lets now turn our attention to the server code. The modified code is given below:

1 # Server code
2 from s o c k e t import ∗
3
4 # The port on which to l i s t en
5 s e rv e r Po r t = 12000
6
7 # Create a TCP s ocke t
8 s e r v e r S o c k e t = s o c k e t ( AF INET ,SOCK STREAM) 9
10 # Bind the s ocke t to the port
11 s e r v e r S o c k e t . bind ( ( ' ' , s e r v e r P o r t ) )

12
13 # Start l istening for incoming connections
14 s e r v e r S o c k e t . l i s t e n ( 1 ) 15
16 print "The s e r v e r i s ready to r e c e i v e " 17
18 # Forever accept incoming connections
19 while 1 :
20 # Accept a connection ; get client ' s s ocke t
21 connection Socket , addr = s e r v e r So c k e t . accept ( )
22
23 # The temporary buffer
24 tmpBuff= "" 25
26 while len ( data ) != 4 0 :
27 # Receive whatever the newly connected client has to send
28 tmpBuff = connectionSocket . recv (40 29
30 # The other side unexpectedly clo s e d i t ' s s ocke t
31 i f not tmpBuff :
32 break
33
34 # Save the data
35 data += tmpBuff
36
37 print data 38
39 # Close the s ocke t
40 connection Socket . c l o se ( )

We made several changes. First, we added loop, while len(data) != 40:, which will spin until the size of data becomes 40. Hence, if recv() is unable to receive the expected 40 bytes after the first call, the loop will ensure that the program calls recv() again in order to receive the remaining bytes. Also, we changed line data = connectionSocket.recv(40) to tmpBuff= connectionSocket.recv(40) and added
test if not tmpBuff: which will evaluate to true if the other side unexpectedly closed it's socket. We then added line data += tmpBuff which adds the newly received bytes to the accumulator file data buffer. These changes ensure that with every iteration the newly received bytes are appended to the end of the buffer.

At this point we have fully functioning server and client programs which do not suffer from the problems discussed above. However, there is still one important caveat: In the above code, what if the server does not know the amount of data the client will be sending? This is often the case in the real world. The answer is that the client will have to tell the server. How? Well, this is where it is up to the programmer to decide on the types and formats of messages that the server and client will be exchanging.

One approach, for example, is to decide that all messages sent from client to server start with a 10 byte header indicating the size of the data in the message followed by the actual data. Hence, the server will always receive the first 10 bytes of data, parse them and determine the size of the data, and then use a loop as we did above, in order to receive the amount of data indicated by the header. You can see such example in the directory Assignment1SampleCodes/Python/sendfile.

Before proceeding, it is recommended that you stop and think about what you just read, experiment with Assignment1SampleCodes/Python/sendfile codes, and make sure you understand everything.

Specifications

The server shall be invoked as:

python serv.py

specifies the port at which ftp server accepts connection requests. For example: python serv.py 1234

The ftp client is invoked as:

cli

is the domain name of the server (ecs.fullerton.edu). This will be converted into 32 bit IP address using DNS lookup. For example: python cli.py ecs.fullerton.edu 1234

Upon connecting to the server, the client prints out ftp>, which allows the user to execute the following commands.

ftp> get (downloads file from the server) ftp> put (uploads file to the server)
ftp> ls (lists files on the server)
ftp> quit (disconnects from the server and exits)

Use two connections for each ftp session - control and data. Control channel lasts throughout the ftp session and is used to transfer all commands (ls, get, and put) from client to server and all status/error messages from server to client. The initial channel on which the client connects to server will be the control channel. Data channel is used for data transfer. It is established and torn down for every file transfer - when the client wants to transfer data (ls, get, or put), it generates an ephemeral port to use for connection and then wait for the server to connect to the client on that port. The connection is then used to upload/download file to/from the server, and is torn down after the transfer is complete.

For each command/request sent from the client to server, the server prints out the message indicating SUCCESS/FAILURE of the command. At the end of each transfer, client prints the filename and number of bytes transferred.

Designing the Protocol

Before you start coding, please design an application-layer protocol that meets the above specifications. Please submit the design along with your code. Here are some guidelines to help you get started:
- What kinds of messages will be exchanged across the control channel?
- How should the other side respond to the messages?
- What sizes/formats will the messages have?
- What message exchanges have to take place in order to setup a file transfer channel?
- How will the receiving side know when to start/stop receiving the file?
- How to avoid over?owing TCP buffers?
- You may want to use diagrams to model your protocol.

Tips
- All sample files are in directory Assignment1SampleCodes. The following files contain sample Python codes, should you choose to use this language.
- Please see sample file, ephemeral.py, illustrating how to generate an ephemeral port number. This program basically creates a socket and calls bind() in order to bind the socket to port 0; calling bind() with port 0 binds the socket to the first available port.
- Please see file cmds.py which illustrates how to run an ls command from your code and to capture its input.
- Subdirectory sendfile contains files sendfileserv.py and sendfilecli.py, illustrating how to correctly transfer data over sockets (this includes large files).

Computer Network & Security, Computer Science

  • Category:- Computer Network & Security
  • Reference No.:- M91965563

Have any Question?


Related Questions in Computer Network & Security

Network project management assignment -purpose of the

Network Project Management Assignment - Purpose of the assessment - The purpose of this assignment is to build a clear understanding of project management fundamentals and their application, specifically: demonstrate pro ...

Advanced wireless networks assignment -wlan design project

Advanced Wireless Networks Assignment - WLAN Design Project - Description: You need to form a group of at most four students, and select one of the case studies provided in Assessment module on VU Collaborate. In this pr ...

Overview of network security assessment - security

Overview of Network Security Assessment - Security Challenges in Emerging Networks Purpose of the assessment - The purpose of this assignment is to develop skills to independently think of innovation. Students will be ab ...

Topic is impacts of data breaches the report will divide in

Topic is "Impacts of data breaches". the report will divide in to 5 section which is : "" 1-Abstract: comprehensive overview of the report in 150 to 200 words. 2- Introduction: Describe the topic and its issue in 250 to ...

What is the difference between a positive economic

What is the difference between a positive economic statement and a normative one.

Objectivespart 1 researching network attacks- research

Objectives Part 1: Researching Network Attacks - Research network attacks that have occurred. - Select a network attack and develop a report for presentation to the class. Part 2: Researching Network Security Audit Tools ...

Assignment -need project made by packet truce follow the

Assignment - Need project made by packet Truce. Follow the given report and please don't make the project big and video will describe full project in details and with configuration. Project - Design and Implementation of ...

You just signed a 30-year lease agreement for a business

You just signed a 30-year lease agreement for a business property. The monthly rent for the first year is $1,000/month, with the ?rst month's rent due today. Starting from the second year onward, the monthly rent will be ...

Question 1 for rsa encryption we need a modulus that is the

Question : 1. For RSA encryption we need a modulus that is the product of two prime numbers, p and q. Assume p = 11 and q = 13, and thus n = p*q = 143. In this case, the RSA encryption exponent e must be relatively prime ...

Buffer overflowthe common vulnerabilities and exposures cve

Buffer overflow The "Common Vulnerabilities and Exposures" (CVE) is a list of cybersecurity vulnerabilities that have been discovered, and they are collected in a database for everyone to see and improve or patch their s ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As