C/C++ Programming Exercise, Exercises of Computer Programming

Basic exercises in C and C++ programming languages.

Typology: Exercises

2021/2022

Uploaded on 07/05/2022

gavin_99
gavin_99 🇦🇺

4.3

(73)

998 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C/C++ Programming Exercise
Getting started
In this exercise you are required to understand, modify and write code in C and/or C++. To start
this exercise download the starter pack from the following URL:
http://www.cl.cam.ac.uk/teaching/current/CandC++/
This ZIP file should contain the following files:
server.c
client.c
rfc0791.txt
rfc0793.txt
message1
message2
message3
message4
Important: You should avoid the use of any compiler-specific features in the exercise. Code
written for this exercise should conform to ISO9899:1999 (“C99”) or ISO14882:1998 (“C++98”)
and use standard libraries. The code should compile and run correctly using gcc or g++ as available
on PWF linux. Your code should not generate any warnings, even with all warnings turned on; in
other words
gcc -std=c99 -Wall --pedantic sourcefile.c
or
g++ -std=c++98 -Wall --pedantic sourcefile.cc
should produce no warnings for all source files you submit.
Exercise Part 1
The exercise starter pack contains two computer programs written in C. The first program, server.c
transmits data and the second program, client.c receives data. There are a few mistakes in these
programs which prevent the code from compiling or functioning correctly. Do the following:
1. Describe in fewer than 100 words the intended functionality of the two programs server.c
and client.c. Write this description into the file answers.txt. The format of this file should
be plain ASCII text.
1
pf3
pf4

Partial preview of the text

Download C/C++ Programming Exercise and more Exercises Computer Programming in PDF only on Docsity!

C/C++ Programming Exercise

Getting started

In this exercise you are required to understand, modify and write code in C and/or C++. To start this exercise download the starter pack from the following URL:

http://www.cl.cam.ac.uk/teaching/current/CandC++/

This ZIP file should contain the following files:

server.c client.c rfc0791.txt rfc0793.txt message message message message

Important: You should avoid the use of any compiler-specific features in the exercise. Code written for this exercise should conform to ISO9899:1999 (“C99”) or ISO14882:1998 (“C++98”) and use standard libraries. The code should compile and run correctly using gcc or g++ as available on PWF linux. Your code should not generate any warnings, even with all warnings turned on; in other words

gcc -std=c99 -Wall --pedantic sourcefile.c or g++ -std=c++98 -Wall --pedantic sourcefile.cc

should produce no warnings for all source files you submit.

Exercise – Part 1

The exercise starter pack contains two computer programs written in C. The first program, server.c transmits data and the second program, client.c receives data. There are a few mistakes in these programs which prevent the code from compiling or functioning correctly. Do the following:

  1. Describe in fewer than 100 words the intended functionality of the two programs server.c and client.c. Write this description into the file answers.txt. The format of this file should be plain ASCII text.
  1. Find and list three programming errors in the code. (There are in fact more than three errors, but you only need to find three.) Modify the code to correct the errors. Compile and execute the code with suitable test data. (Hint: the IP address 127.0.0.1 can be used as a suitable IP address if you want to run both the client and server components on the same machine.) A list of the three errors should be appended to the file answers.txt. Your corrected code should be saved into server1.c and client1.c.

Exercise – Part 2

A computer scientist writes a packet inspector to record every bit of data sent by (a corrected version of) server.c on machine A and received by (a corrected version of) client.c on machine B. The recorded data contains all the TCP/IP packets and includes the TCP and IP headers in network byte order together with the actual message payload.

The files rfc0791.txt and rfc0793.txt contain the specification of IP and TCP headers respec- tively. Your attention is drawn in particular to the layout, in bytes, of the contents of the header files as expressed in these documents. For convenience, the layouts are also shown in the Appendix.

The computer scientist invokes the server program on machine A and the client program on machine B once, and generates a log file of all the packets sent between the two machines. The log file contains the raw packet data in the order in which the packets were transmitted on machine A. Therefore the first packet is at the start of the file, and this is immediately followed by data from the second packet, and so on.

An example log file is message1. Your next task is to write a C or C++ program to read in any log file in this format and determine the following facts:

  • The IP addresses of machines A and B (source and destination)
  • The value of the first IP packet’s header size field (IHL – IP header length)
  • The length of the first IP packet in bytes (total length)
  • The value of the first TCP packet’s header size field (data offset)
  • The total number of IP packets in the trace

You should save your program into a file called summary.c or summary.cc. Your program should output the above details in the same order as shown above by printing to stdout on a single line, using spaces to separate the fields. For example, if the IP addresses of machines A and B are 192.168.1.1 and 192.168.1.2, the header length field value is 6 , the length of the IP packet is 52 bytes, the value in the TCP header length is 5 , and the total number of packets in the trace is 20 , your program should output:

Appendix

RFC 791 (http://www.ietf.org/rfc/rfc0791.txt) specifies the layout of packets in the IP Pro- tocol. You may need to read portions of this document in order to complete this exercise. Of particular interest is the position of the packets in the Internet Protocol header:

|Version| IHL |Type of Service| Total Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identification |Flags| Fragment Offset | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Time to Live | Protocol | Header Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Destination Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

RFC 793 (http://www.ietf.org/rfc/rfc793.html) specifies the layout of packets using the TCP protocol. You may need to read portions of this document in order to complete this exercise. Of particular interest is the position of the key data elements in the Transmission Control Protocol header:

| Source Port | Destination Port | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Sequence Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Acknowledgment Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Data | |U|A|P|R|S|F| | | Offset| Reserved |R|C|S|S|Y|I| Window | | | |G|K|H|T|N|N| | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Checksum | Urgent Pointer | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | data | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+