School of Computing and Information Systems
COMP30023: Computer Systems
Practical Week 9
1 Socket Programming in C
Copyright By PowCoder代写 加微信 powcoder
Sockets are the endpoints used by applications to send messages from one host to another. The socket of a TCP connection is defined by its 5-tuple, the protocol (TCP), source IP address, source port, destination IP address, and destination port. Most operating systems provides an application programming interface (API) for socket programming, abstracting the lower layers of the protocol stack.
The listings server.c and client.c provide code for implementing server/client communication with TCP. You may want to study this code as it can be helpful to you in your project.
1. Compile and run on your VM. Use a port between 8000-9000 when running the program. Command:
2. Without terminating the server program, now create another new tab/window in your SSH client program (or open another terminal window) and connect via SSH to your VM. You should now have two SSH sessions for your VM. You can use one of these sessions to run the server and the other to run the client.
3. On the client session that you just created, connect via Netcat to the custom server.
$ nc
You should now be able to send a message from the client and receive it on the server.
4. Now use the provided client program to connect to the server instead.
Start the server program again.
In the client session, compile and run . Command:
Note: When you restart the server after interacting with a client, you may get the error
ERROR on binding: Address already in use
If this is the case, switch to another port before starting the server again.
1.1 netstat
Netstat can be a valuable debugging tool. A common use case is to use it to display all the open ports on your machine. Let¡¯s use it to identify our server process.
1. Run the server again.
Run the command $ sudo netstat -tulnp
Notice how we are able to view which processes are listening on which ports.
2. Identify the process ID (or PID) of your server, which is running on port 8198.
3. Kill the the server process using the PID, with command: , which sends SIGINT (like Ctrl-C)
can also be used, to terminate processes immediately (cannot be caught or ignored)
4. Use the netstat command to observe that there is now no process listening on port 8198.
$ gcc server.c -o server
$ ./server 8198
gcc client.c -o client
$ ./client
$ kill -2 PID
$ kill -9 PID
1.2 Coding Tasks (basic)
As you are now familiar with socket programming in C, you should try adding the following functionalities to the client and server programs.
1. From the server program, convert the received client message to uppercase and send it to back to the client. The client should print the converted message received from the server.
2. The current implementation uses non-persistent TCP connection (Think: Why?), your job is to make the connection persistent so that the client can keep sending messages.
3. As you have implemented a persistent TCP connection for both the client and the server, there is no way that the server/client can know when to close the TCP connection. Therefore, implement a termination protocol by sending a special message GOODBYE-CLOSE-TCP from the client to the server. After sending this special message, the client should close the TCP connection. In addition, upon receiving this special message, the server should also close the TCP connection.
4. Instead of exchanging messages, send the contents of any .txt file from the client to the server and save the contents as a text file on the server. Start from the original client.c, server.c.
1.3 Coding Tasks (Extension)
If the above tasks are too easy for you, have some fun implementing a custom FTP protocol to Download/Upload any text files to the server. We are calling it a custom FTP protocol as we are not following the RFC. Instead, we are making our own rules on how the client and server should communicate. The details of the custom FTP protocol are as follows:
1. Client Message: [DOWNLOAD FILE-NAME]. This message is for downloading a file from the server. If the server receives this client message and the requested file exists on the server, it will send a message containing [OK FILE-CONTENT] to the client and the client should save the FILE-CONTENT as FILE-NAME. If the file doesn¡¯t exist in the server, the server should send a message containing [NOT- FOUND] to the client.
2. Client Message: [UPLOAD FILE-NAME FILE-CONTENT]. This message is for uploading the contents of a file from the client¡¯s machine to the server. At first, when a user types this message, the client will read the contents of the local file to generate the message. Then it will send the message to the server. Upon receiving this message, the server will save the FILE-CONTENT in a file named FILE-NAME.
* If you are too passionate about protocol and RFCs, you are welcome to implement a complete FTP server1 .
Telnet + HTTP (Bonus)
Web browsers communicate with web servers by issuing HTTP Requests.
In this activity, we will manually send a HTTP request using telnet, which allows for interactive (insecure) remote communication via text.
1. Establish a Telnet connection to the server at example.com on port 80.
If you¡¯re on a 172.26.128.0/20 VM, use the host cis.unimelb.edu.au instead. e.g. $ telnet example.com 80
2. Let¡¯s issue a well-formed HTTP version 1.1 (HTTP/1.1) GET request to the index page.
Press enter twice after entering the text (once to signal the end of the Host header, and another time to signal the end of header fields). e.g.
GET / HTTP/1.1
Host: example.com
3. Inspect the result.
3 Resources
Beej¡¯s Guide to Network Programming: https://beej.us/guide/bgnet/html/ 1 https://tools.ietf.org/html/rfc959
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com