This project simulates a basic FTP file transfer over a network, demonstrating the flow of data through different layers of the TCP/IP model.
The project is divided into a client and a server, each with several classes that handle different aspects of the simulation.
-
FtpServer: The main entry point for the server side.main(): Creates aServerSocketand listens for incoming client connections on port 8080. When a client connects, it creates a newClientHandlerthread to handle the connection, allowing the server to handle multiple clients concurrently.
-
ClientHandler: Manages a single client connection on the server side.run(): This is the entry point for the new thread. It gets the input stream from the client's socket. It then uses aFileHandlerto process the incoming file data. It also calls methods fromNetworkLayerSimulatorto print the simulated network layer activity.
-
FileHandler: Responsible for the application-level logic of receiving the file.receiveFile(): Reads the file metadata (name and size) from the input stream. Then, it reads the file content from the stream and writes it to a new file namedreceived_<original_filename>.
-
Sender: The main entry point for the client side.main(): Parses command-line arguments for the server IP and the path to the file to be sent. It establishes a socket connection to the server. It then writes the file's name and size to the output stream, followed by the file's content. It also calls methods fromNetworkLayerSimulatorto print the client-side network activity.
-
NetworkLayerSimulator: A utility class that simulates the TCP/IP stack.- Contains a series of static methods (
printClient...andprintServer...) that are called from theSenderandClientHandlerto print messages to the console. These messages illustrate the data being "passed" through the Application, Transport, Network, and Data Link layers.
- Contains a series of static methods (
This diagram shows the flow of execution between the client and server components.
graph TD
subgraph Client
A["Sender.main()"] --> B{"Connect to Server"};
B --> C["Send File Metadata"];
C --> D["Send File Content"];
end
subgraph Server
E["FtpServer.main()"] --> F{"Accept Connection"};
F --> G["Create ClientHandler Thread"];
G --> H["ClientHandler.run()"];
H --> I{"Receive File Metadata"};
I --> J["Create FileHandler"];
J --> K["FileHandler.receiveFile()"];
K --> L["Save File to Disk"];
end
D -- "TCP Stream" --> I;
subgraph "Simulation (Console Output)"
A --> S1["printClient..."];
H --> S2["printServer..."];
end
-
Compile the source code:
javac src/*.java -
Start the server:
java -cp src FtpServer
-
In a separate terminal, send a file from the client:
java -cp src Sender <server_ip> <file_path>
- Replace
<server_ip>with the IP address of the server (e.g.,127.0.0.1). - Replace
<file_path>with the path to the file you want to send.
- Replace
-
Observe the output in both terminals to see the simulated network traffic.