Skip to content

rajzzz/network-sim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Java FTP Simulation

This project simulates a basic FTP file transfer over a network, demonstrating the flow of data through different layers of the TCP/IP model.

Architecture

The project is divided into a client and a server, each with several classes that handle different aspects of the simulation.

Core Classes

  • FtpServer: The main entry point for the server side.

    • main(): Creates a ServerSocket and listens for incoming client connections on port 8080. When a client connects, it creates a new ClientHandler thread 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 a FileHandler to process the incoming file data. It also calls methods from NetworkLayerSimulator to 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 named received_<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 from NetworkLayerSimulator to print the client-side network activity.
  • NetworkLayerSimulator: A utility class that simulates the TCP/IP stack.

    • Contains a series of static methods (printClient... and printServer...) that are called from the Sender and ClientHandler to print messages to the console. These messages illustrate the data being "passed" through the Application, Transport, Network, and Data Link layers.

Architectural Diagram

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
Loading

Usage

  1. Compile the source code:

    javac src/*.java
  2. Start the server:

    java -cp src FtpServer
  3. 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.
  4. Observe the output in both terminals to see the simulated network traffic.

About

An FTP program which simulates File transfer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages