辅导案例-CS176A

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
2020/10/18 https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html
https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html 1/6
CS176A - HOMEWORK ASSIGNMENT #1
Due: October 22, 2020 (By 11:59pm)
Objectives
There are a number of objectives to this assignment. The first is to make sure you have some experience
developing a network-based socket application. Second, it will help you "tune up" your programming
skills and prepare you for the other upper-division networking courses. Third, because you are allowed to
use any references you find on the Internet (including copies of existing code!), this assignment will help
you see just how many network programming aids are available via the web. And finally, having just a bit
of practical experience will put a lot of the protocol concepts we learn into perspective.
Assignment Details
The goal of this assignment is to implement a simple client-server system. You can use C, Python or Java.
The basic functionality of the system is to exchange commands and output between a client and server.
Please be sure to follow all instructions when writing your assignment.
Your server will start in a "passive mode", in other words, it will listen to a specified port for commands
from the client. Separately, the client will start and then will connect to the server on a given host name or
IP address plus a port number. The client should take as input from the user the host name or IP address
plus the port number and a command to be executed. Consider issuing a command that will require to
respond with a relatively large file, allowing you to test transmission of multiple packets (see TCP and
Reliable UDP below for more details).
The client, when started, should request the following information:
1. Enter server name or IP address:
2. Enter port:
3. Enter command:

and should accept a single line for each query.
The client should make sure that the specified port is valid, in other words, the client should check if the
port is in the range from 0 to 65535 and if not to return a message of the format "Invalid port number."
The client, then, will try to connect to the specified server on the specified port and transmit the command.
The general interaction between the client and server is as follows:

1. The server starts and waits for a connection to be established by the client.
2. When a command is received, then the server will:
Issue the command to its system and store the output in a file.
Open the file, read its content to a buffer.
Write the buffer contents to the connection established by the client.
3. The client will receive the data from the socket and store it in a local file.
Note that you have to handle network errors. For example, make sure you verify in your client code that
the connection was successful and if not, return an error. Use the following message: "Could not connect
to server."
Also make sure your client returns an error if the response to the command was not successfully received.
Use the following error message: "Did not receive response."
2020/10/18 https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html
https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html 2/6
TCP and Reliable UDP
You will implement your client and server using both TCP and UDP. This means you will be writing
four different programs. (The clients and servers using TCP and UDP won't be interoperable so you
won't be able to use the TCP client with the UDP server and vice versa. Why not?) Using TCP
should be more straightforward since it has connections, reliability, in-order delivery, etc. However,
UDP by itself does not include any of these features so you will have to add a simple form of
reliability. You will do this using stop-and-wait reliability. Since we are working at the application
level and there is no concept of packets, we will implement our stop and wait reliability at the level
of character strings.
The client and server with Reliable UDP will operate as follows:
1. Your client will format the command to be executed and send the length of the string to the
server (a "length" message). Then, in a separate message, it will send the command string.
(NOTE: to simplify the assignment, commands are guaranteed to be less than about 1500
bytes so the command will always fit into one packet.)
2. When the server receives the length message it will wait up to 500 milliseconds for that
number of bytes to be sent. If it receives the correct number of bytes, it will respond with a
string containing the characters "ACK" (an "acknowledgement"). If it does not receive the
correct number of bytes by the end of the timeout period, it will give up on this message and
issue a message informing the user that obtaining instructions failed. This message should be
in the format "Failed to receive instructions from the client." After that the server should
remain idle, waiting for another length message to be sent.
3. If your client has not received an ACK within 1 second it will resend the length value and
then the request message again.
4. Your client will resend, up to 3 times, before giving up, closing the connection and
terminating. Upon closing connection, the client should display the following message:
"Failed to send command. Terminating."
5. The server must be capable of sending a response containing more data than what will fit into
a single packet. The server will implement reliability using stop-and-wait. This means the file
will be divided into packets and every packet must be ACKed in order to preserve the stop-n-
wait characteristics of the protocol. First, the server will compute the size of the data it has to
send and then will send it in a "length" message. The server will then send messages
containing the output one by one and will wait for an ACK for each message sent and before
the next, new message is sent. Similar to the client, the server will retry sending single
message up to three times, before giving up. If the server does not receive an ACK message
after three attempts, it should issue a message informing the user that output transmission
failed. Use the format "File transmission failed."
6. Finally, upon successful reception of the output, the client should display the following
message "File filename saved."
NOTE: When you are sending large amounts of data over UDP, the data might be sent in
multiple portions. This occurs because the UDP buffer fills up. So what can happen is that
even though your server says that it is sending 5120 bytes, the UDP on the client delivers the
data in, for example, 512 byte chunks because of the buffer size. The client receives the first
512 byte chunk and compares it to the 5120 bytes the server said it was sending and the two
values do not match. Because the amount of data expected is not equal to the amount of data
received, the client does not send an ACK. The client receives the next 512 byte chunk and
compares it to the 5120 bytes the server said it was sending. These values do not match, so
2020/10/18 https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html
https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html 3/6
the client does not ACK, etc. This behavior can cause the sender to continually resend until it
times out. To solve this problem the receiver must wait for all of the data to be received
before comparing it to the amount sent. This problem will probably not occur when your
client sends data to your server because the entire command will fit into a single packet and
thus it will arrive all at once. However, you will want to test your system with large enough
files.
Examples
NOTE: you can run server and client locally on one machine and use localhost for the IP address.
Starting the Server
Assume you are working on your personal computer. You should start the server by issuing a
command in a terminal, which syntax should be as follows (see below for correct naming for
executables):
username>server 3300
where server is the executable for the program you wrote and 3300 is the port number you
chose to listen to.
Client Input/Output Example
Assume you are working on your personal computer. You should start the client by issuing a
command in a terminal, which syntax should be as follows (see below for correct naming for
executables):
username> client
where client is the executable for the program you wrote.
Here are some other examples of what the client might see depending on the input.
username> client
Enter server name or IP address: gizmo.cs.ucsb.
Enter port: 12321
Could not connect to server.
username>
or
username> client
Enter server name or IP address: 428.163.7.19
Enter port: 12321
Could not connect to server.
username>
or

username> client
Enter server name or IP address: 127.0.0.1
2020/10/18 https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html
https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html 4/6
Enter port: 99999
Invalid port number.
username>
or
username> client
Enter server name or IP address: 127.0.0.1
Enter port: 3300
Enter command: cat /var/log/syslog > syslog.otp
Failed to send command. Terminating.
username>
or
username> client
Enter server name or IP address: 127.0.0.1
Enter port: 3300
Enter command: cat /var/log/syslog > syslog.otp
Did not receive response.
username>
or
username> client
Enter server name or IP address: 128.163.7.19
Enter port: 3300
Enter command: cat /var/log/syslog > syslog.otp
File syslog.otp saved.
username>
username>cat syslog.otp
Sep 27 07:47:45 labgizmo rsyslogd: [origin software="rsyslogd" swVersion="4.2.0" x-
pid="691" x-info="http://www.rsyslog.com"] rsyslogd was HUPed, type 'lightweight'.
Sep 27 07:48:34 labgizmo anacron[25673]: Job `cron.daily' terminated
Sep 27 07:48:34 labgizmo anacron[25673]: Normal exit (1 job run)
.....
username>
Server Output Example
username>server 3300
Failed to receive instructions from the client.

or

username>server 3300
File transmission failed.
2020/10/18 https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html
https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html 5/6
Language Choice and File Names
You will choose C, Python or Java for this assignment. Regardless of the language choice, you must
turn in exactly four programs.
For C, the programs should be:
Client in C using TCP: client_c_tcp.c
Server in C using TCP: server_c_tcp.c
Client in C using UDP: client_c_udp.c
Server in C using UDP: server_c_udp.c
To compile your C code, use the following four commands:
"gcc -g -o client_c_tcp client_c_tcp.c"
"gcc -g -o server_c_tcp server_c_tcp.c"
"gcc -g -o client_c_udp client_c_tcp.c"
"gcc -g -o server_c_udp server_c_tcp.c"
For Java, the program names should be:
Client in Java using TCP: client_java_tcp.java
Server in Java using TCP: server_java_tcp.java
Client in Java using UDP: client_java_udp.java
Server in Java using UDP: server_java_udp.java
To compile your Java code, use the following commands:
"javac client_java_tcp.java"
"javac server_java_tcp.java"
"javac client_java_udp.java"
"javac server_java_udp.java"
For Python, the program names should be:
Client in Python using TCP: client_python_tcp.py
Server in Python using TCP: server_python_tcp.py
Client in Python using UDP: client_python_udp.py
Server in Python using UDP: server_python_udp.py
To compile your Python code, use the following commands:
"python client_python_tcp.py"
"python server_python_tcp.py"
"python client_python_udp.py"
"python server_python_udp.py"
NOTE: Pay attention to all of these directions carefully. Make sure you name your files and format your
messages as specified in the assignment. An automated program will be used for grading and if there are
any deviations, you will lose points!(There is a CS176A-related point to this note as well: as you will
learn, protocols are absolutely precise, there can be no deviation in syntax. Programming this assignment
is straightforward, but those who earn a 100 will have followed the directions exactly.)
Grading Guidelines
2020/10/18 https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html
https://sites.cs.ucsb.edu/~almeroth/classes/F20.176A/hw/f20-176-hw1.html 6/6
You may use pieces of code from the Internet to help you do this assignment (e.g. basic socket code).
However, this is just like citing a passage from a book, so if you copy code, you must cite it. To do this,
put a comment at the beginning of your code that explains exactly what you have copied, who originally
wrote it, and where it came from.
Below is a breakdown of points for this assignment. In addition to correctness, part of the points count
towards how well code is written and documented. NOTE: good code/documentation does not imply that
more is better. The goal is to be efficient, elegant and succinct!
30 pts: Clients
50 pts: Servers
20 pts: Documentation/Proper References
Final Warning
This is an assignment you definitely want to start on early. The design of the assignment is such that it is
nearly impossible to provide all of the details you need. Instead of assuming things should be done a
particular way, ask questions! Use every opportunity to reach out to instructors in Piazza and ask your
questions.
Assignment Turnin
The assignment should be submitted using the course GauchoSpace site. Because the web site is set to
only allow one file to be submitted, you should use a commonly available tool (tar or zip) to combine your
four files into a single archive file.
Cheating Policy
This assignment is to be done individually. Cheating will not be tolerated. Please read the UCSB Academic
Code of Conduct to find out more about Student Conduct and Discipline.

欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468