EXPERIMENT: NO: 4
AIM: Simulating HTTP, FTP, and DBMS access in a network involves modelling the
behavior of these protocols and their interactions with network resources.
#include <iostream>
#include <queue>
#include <vector>
#include <random>
#include <chrono>
#include <thread>
// Constants for simulation
const int SIMULATION_TIME = 100; // Simulation time in seconds
const int HTTP_PORT = 80;
const int FTP_PORT = 21;
const int DBMS_PORT = 3306;
// Simulated network packet
struct Packet {
int source;
int destination;
int port;
std::string data;
std::chrono::steady_clock::time_point start_time;
};
// Simulated server
class Server {
private:
int port;
std::queue<Packet> request_queue;
std::mt19937 rng;
std::uniform_int_distribution<int> processing_time;
public:
Server(int p, int min_time, int max_time)
: port(p), rng(std::random_device{}()), processing_time(min_time, max_time) {}
void receiveRequest(Packet packet) {
request_queue.push(packet);
void processRequests() {
while (!request_queue.empty()) {
Packet packet = request_queue.front();
request_queue.pop();
// Simulate processing time
int time = processing_time(rng);
std::this_thread::sleep_for(std::chrono::milliseconds(time));
// Calculate response time
auto end_time = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time -
packet.start_time).count();
std::cout << "Server on port " << port << " processed request from " << [Link]
<< " in " << duration << " ms" << std::endl;
};
// Simulated client
class Client {
private:
int id;
std::mt19937 rng;
std::uniform_int_distribution<int> request_interval;
std::uniform_int_distribution<int> service_type;
public:
Client(int i, int min_interval, int max_interval)
: id(i), rng(std::random_device{}()), request_interval(min_interval, max_interval),
service_type(0, 2) {}
void sendRequest(std::vector<Server>& servers) {
while (true) {
// Simulate request interval
int interval = request_interval(rng);
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
// Choose a service type (HTTP, FTP, DBMS)
int service = service_type(rng);
int port = (service == 0) ? HTTP_PORT : (service == 1) ? FTP_PORT :
DBMS_PORT;
// Create a packet
Packet packet;
[Link] = id;
[Link] = port;
[Link] = port;
[Link] = "Request data";
packet.start_time = std::chrono::steady_clock::now();
// Send packet to the appropriate server
servers[service].receiveRequest(packet);
std::cout << "Client " << id << " sent request to server on port " << port << std::endl;
};
int main() {
// Create servers for HTTP, FTP, and DBMS
std::vector<Server> servers;
servers.emplace_back(HTTP_PORT, 100, 500); // HTTP server with processing time
100-500 ms
servers.emplace_back(FTP_PORT, 200, 1000); // FTP server with processing time
200-1000 ms
servers.emplace_back(DBMS_PORT, 300, 1500); // DBMS server with processing time
300-1500 ms
// Create clients
std::vector<Client> clients;
for (int i = 0; i < 5; ++i) {
clients.emplace_back(i + 1, 500, 2000); // Clients with request intervals of 500-2000 ms
// Start client threads
std::vector<std::thread> client_threads;
for (auto& client : clients) {
client_threads.emplace_back(&Client::sendRequest, &client, std::ref(servers));
// Simulate for a fixed time
auto start_time = std::chrono::steady_clock::now();
while (true) {
auto current_time = std::chrono::steady_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::seconds>(current_time -
start_time).count();
if (elapsed_time >= SIMULATION_TIME) {
break;
// Process requests on servers
for (auto& server : servers) {
[Link]();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Join client threads
for (auto& thread : client_threads) {
[Link]();
std::cout << "Simulation completed." << std::endl;
return 0;
}
Explanation:
1. Packet Structure:
o The Packet struct represents a network packet with fields for source,
destination, port, data, and start time.
2. Server Class:
o The Server class simulates a server that listens on a specific port. It receives
requests, processes them with a random delay, and calculates the response
time.
3. Client Class:
o The Client class simulates a client that sends requests to servers at random
intervals. It randomly chooses between HTTP, FTP, and DBMS services.
4. Simulation:
o The main function creates servers for HTTP, FTP, and DBMS, and multiple
clients. Clients send requests to servers, and servers process these requests.
The simulation runs for a fixed time (SIMULATION_TIME).
5. Threading:
o Clients run in separate threads to simulate concurrent requests. Servers process
requests in the main thread.
Notes:
● This is a simplified simulation and does not account for many real-world factors like
packet loss, network congestion, or complex protocol interactions.
● The processing times and request intervals are randomized to simulate variability in
network behavior.
● The simulation runs for a fixed duration (SIMULATION_TIME), after which it
terminates.
This program provides a basic framework for simulating HTTP, FTP, and DBMS access in a
network. You can extend it to include more realistic features and behaviors.