Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem is that, i am not understanding how to simulation work and where i should start. i never did network simulation till ,only reading computer network subject and never did any practical.

What I have tried:

i saw the youtube video of network simulation but i have not done any practical so i want to ask you where should i start.
Posted
Updated 8-Jul-19 22:18pm
Comments
wseng 8-Jul-19 3:50am    
Do you have any requirements?

1 solution

I suggest implementing the real thing first, before trying a simulation. If you don't understand what kind of functionality you need to make it work, there is no point to write a simulation.

Depending on what kind of network you're thinking of, try google to find some code examples.

Once you know what you need, write an (interface) class that provides all the functions that you need, and then derive an implementation class from it that overrides these functions with simulated behavior. How this behavior should look like depends on what you want to simulate.

Here's a basic design in C++:
C++
#include <iostream>
#include <string>
class NetworkBase {
public:
    virtual ~NetworkBase() {}
    virtual int connect(const std::string& address) = 0;
    virtual int send(const std::string& message) = 0;
    virtual std::string receive() = 0;
    virtual int disconnect() = 0;
};
class NetworkSimulation : public NetworkBase {
private:
    int state_;
    std::string address_;
public:
    NetworkSimulation() : state_(0) {}
    virtual ~NetworkSimulation() {}
    virtual int connect(const std::string& address) {
        if (address.empty())
            return -1; // error!
        address_ = address;
        state_ = 1;
        return 0; // succcess
    }
    virtual int send(const std::string& message) {
        std::cout << "Sending new message to " << address_ << ": " << message<< std::endl;
        return (int)message.size();
    }
    virtual std::string receive() {
        std::string msg;
        std::cout << "Reading message from " << address_ << std::endl;
        std::getline(std::cin, msg);
        std::cout << "Message received from " << address_ << ": " << msg << std::endl;
        return msg;
    }
    virtual int disconnect() {
        if (state_ == 0)
            return -1; // error!
        address_.clear();
        state_ = 0;
        return 0; // succcess
    }
};

int main()
{
    NetworkBase* my_network = new NetworkSimulation;
    std::string address("there");
    int error = my_network->connect(address);
    if (error != 0) {
        std::cout << "Not connected to " << address << std::endl;
    }
    else {
        int msg_size = my_network->send("Hello");
        if (msg_size == 0) {
            std::cout << "Error sending message to " << address << std::endl;
        }
        std::string msg = my_network->receive();
        std::cout << "Message received from " << address << ": " << msg << std::endl;
        error = my_network->disconnect();
        if (error != 0) {
            std::cout << "Error disconnecting from " << address << std::endl;
        }
    }
    delete my_network;

    return 0;
}
 
Share this answer
 
Comments
Member 14523745 10-Jul-19 14:55pm    
Thank you Sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900