Computer
Networks
Report by: Noureldin Allam
Submission date: 31 / 03 / 2022
File Name: Computer Networks Report
Student ID: 22205087
Candidate number: 266454
Word count: 1522
TFTP-TCP Server
This code is a basic implementation of a Trivial File Transfer Protocol (TFTP) server in Java. TFTP is a
simple file transfer protocol that operates on top of UDP and is commonly used for bootstrapping firmware
updates, configuration files, and other small files.
Here is a brief explanation of the code:
I. The TFTP_Server class contains the main method and sets up a ServerSocket to listen for incoming
client connections on port 69, the default port for TFTP.
II. The main method enters an infinite loop that accepts incoming client connections using the
ServerSocket.accept() method. For each incoming client connection, a new thread is created and
started to handle the connection using the ClientHandler class. The ClientHandler class implements
the Runnable interface and handles each client connection in a separate thread. When a client
connection is accepted, the run method is called. Then in the run method, the client's input and
output streams are created using DataInputStream and DataOutputStream, respectively. The
readInt() method is used to read the client's request type (either 1 for file upload or 2 for file
download) from the input stream.
III. If the client's request type is 1 (file upload), the readUTF() method is used to read the filename from
the input stream, and a FileOutputStream is created to write the contents of the file. A buffer of
1024 bytes is used to read the file data from the input stream and write it to the output stream until
the end of the file is reached.
IV. If the client's request type is 2 (file download), the readUTF() method is used to read the filename
from the input stream. If the file does not exist, an error message is sent to the client using the
writeUTF() method, and the thread is terminated. If the file exists, the writeLong() method is used
to send the file size to the client, and a FileInputStream is created to read the contents of the file. A
buffer of 1024 bytes is used to read the file data from the input stream and write it to the output
stream until the end of the file is reached.
V. The finally block is used to ensure that the client's socket is closed when the thread is terminated.
TFTP-TCP-Client
This code is an implementation of a TFTP (Trivial File Transfer Protocol) client that can send or receive
files from a TFTP server.
I. The client starts by creating a socket and connecting to the server on the specified port (69 in this
case). It then creates input and output streams for sending and receiving data to and from the server.
II. The user is prompted to input a choice of either storing or retrieving a file. If the user chooses to
store a file, they are prompted to input a filename and the contents of the file. The file contents are
read from the standard input (System.in) and sent to the server using the output stream. If the user
chooses to retrieve a file, they are prompted to input a filename. The filename is sent to the server
using the output stream, and the size of the file is read from the input stream. If the file is found, its
contents are received from the server and written to a local file using a FileOutputStream.
III. Finally, the socket is closed, terminating the connection to the server.
TFTP-UDP-Server
This code is a Java implementation of a TFTP (Trivial File Transfer Protocol) server, which allows file
transfers over the network. The server listens on port 69 and can handle read and write requests from clients.
The code defines some constants, including the TFTP port number, a buffer size, and two types of packets
(ACK and ERROR) that the server will send to clients. The code also defines a default directory to use for
file transfers.
I. The main class, TFTPServer, includes a nested class RequestHandler, which implements the
Runnable interface. Each instance of RequestHandler is responsible for handling a single client
request. When a request is received, the run method of the RequestHandler class is executed.
II. The run method first parses the request opcode, to see whether the request is a read or write request,
and then proceeds accordingly..
If the opcode is 1, which indicates a read request, the code checks if the requested file exists in the server's
root directory. If it does, the server sends the first block of data (of size BUFFER_SIZE - 4 bytes) to the
client. The server then enters a loop where it waits for acknowledgment packets from the client, and sends
subsequent data blocks until the entire file is transferred or an error occurs.
If the opcode is 2, which indicates a write request, the code checks if the requested file already exists in the
server's root directory. If it does not exist, the server sends an acknowledgment packet to the client,
indicating that it is ready to receive the file's data. The server then enters a loop where it receives data
packets from the client, writes the data to a file on the server, and sends acknowledgment packets back to the
client until the entire file is transferred.If the opcode is neither 1 nor 2, the server sends an error packet to the
client indicating that the request is invalid.
III. The parseFilename, parseBlockNumber, and parseDataLength methods are used to extract
information from the TFTP packets received from clients. Overall, this code provides a basic TFTP
server implementation that can handle simple file transfers over the network.
IV. The createDataPacket method takes three parameters: blockNumber, dataBuffer, and
dataLength. It creates a TFTP data packet with the specified block number, data buffer, and data
length. It initializes a byte array called packetBuffer with a size of BUFFER_SIZE. It sets the first
two bytes of packetBuffer to 0 and 3, which indicates that it is a data packet. It sets the next two
bytes to the block number by shifting the blockNumber value 8 bits to the right and then doing a
bitwise AND operation with 0xff. Finally, it copies the data from the dataBuffer to packetBuffer
starting at index 4 and ending at the specified dataLength. The method returns the packetBuffer.
V. The createAckPacket method takes one parameter: blockNumber. It creates a TFTP
acknowledgment packet with the specified block number. It initializes a byte array called
packetBuffer with a size of 4. It sets the first two bytes of packetBuffer to 0 and 4, which indicates
that it is an acknowledgment packet. It sets the next two bytes to the block number by shifting the
blockNumber value 8 bits to the right and then doing a bitwise AND operation with 0xff. The
method returns the packetBuffer.
VI. The main method starts the TFTP server on a specified port and root directory. It initializes a
DatagramSocket called serverSocket on the specified port. It prints a message indicating that the
server has started. It then enters a loop that listens for incoming TFTP requests. When a request is
received, it creates a RequestHandler object and passes it the serverSocket, the request packet, and
the root directory. It then calls the run method of the RequestHandler object.
TFTP-UDP-Client
This is a Java program that implements a Trivial File Transfer Protocol (TFTP) client. TFTP is a simple
protocol that allows clients to transfer files to and from a server.
The TFTPClient class contains a main method that reads user input to either store a file or retrieve a file, or
to exit the program. If the user selects to store a file, the sendWriteRequest method is called. If the user
selects to retrieve a file, the sendReadRequest method is called.
The sendWriteRequest method takes in a DatagramSocket object, the address of the TFTP server, and a
BufferedReader object. It prompts the user for the name of the file to be stored and creates a
ByteArrayOutputStream to construct a write request message to send to the TFTP server. The method
then sends the write request message to the server and waits for acknowledgement packets from the server.
It reads data from the file and constructs data packets to send to the server, and waits for acknowledgements
from the server before sending the next packet. If an error occurs, the method prints an error message and
returns.
The sendReadRequest method takes in a DatagramSocket object, the address of the TFTP server, and a
BufferedReader object. It prompts the user for the name of the file to be retrieved and creates a
ByteArrayOutputStream to construct a read request message to send to the TFTP server. The method then
sends the read request message to the server and waits for data packets from the server. It writes the data
from each data packet to a file, sends acknowledgement packets to the server, and continues receiving data
packets until the entire file has been received. If an error occurs, the method prints an error message and
returns.