cpp

exercises

exercises.cpp⚙️
/**
 * Networking - Exercises
 * Compile: g++ -std=c++17 -Wall -pthread exercises.cpp -o exercises
 */

#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;

// ============================================================
// Exercise 1: Simple HTTP GET ⭐⭐
// ============================================================

// TODO: Send HTTP GET request and receive response
// Connect to a web server, send "GET / HTTP/1.1\r\n\r\n"

void exercise1() {
    // string response = httpGet("example.com", 80, "/");
    // Print response headers
    cout << "(Implement HTTP GET client)" << endl;
}

// ============================================================
// Exercise 2: Chat Server ⭐⭐
// ============================================================

// TODO: Multi-client chat server
// - Accept multiple clients
// - Broadcast messages to all connected clients

void exercise2() {
    // ChatServer server(8888);
    // server.run();  // Clients can connect and chat
    cout << "(Implement chat server)" << endl;
}

// ============================================================
// Exercise 3: File Transfer ⭐⭐⭐
// ============================================================

// TODO: Send file over TCP
// - Server: receive and save file
// - Client: send file contents

void exercise3() {
    // sendFile("localhost", 8888, "file.txt");
    // receiveFile(8888, "received.txt");
    cout << "(Implement file transfer)" << endl;
}

// ============================================================
// Exercise 4: UDP Ping-Pong ⭐⭐
// ============================================================

// TODO: UDP client sends "PING", server responds "PONG"
// Measure round-trip time

void exercise4() {
    // UDPPing client;
    // double rtt = client.ping("localhost", 8888);
    // cout << "RTT: " << rtt << " ms" << endl;
    cout << "(Implement UDP ping)" << endl;
}

// ============================================================
// Exercise 5: Port Scanner ⭐⭐⭐
// ============================================================

// TODO: Scan range of ports on host
// Report which ports are open

void exercise5() {
    // auto openPorts = scanPorts("localhost", 1, 1024);
    // for (int port : openPorts) cout << port << " is open" << endl;
    cout << "(Implement port scanner)" << endl;
}

// ============================================================
// Exercise 6: Simple DNS Lookup ⭐⭐
// ============================================================

// TODO: Resolve hostname to IP address using getaddrinfo

void exercise6() {
    // string ip = resolve("www.example.com");
    // cout << "IP: " << ip << endl;
    cout << "(Implement DNS lookup)" << endl;
}

// ============================================================
// Exercise 7: HTTP Server ⭐⭐⭐
// ============================================================

// TODO: Simple HTTP server that serves static files
// Parse HTTP requests, send proper responses

void exercise7() {
    // HTTPServer server(8080, "./www");
    // server.run();
    cout << "(Implement simple HTTP server)" << endl;
}

// ============================================================
// Exercise 8: Connection Pool ⭐⭐⭐
// ============================================================

// TODO: Maintain pool of reusable connections
// - getConnection() returns available or creates new
// - releaseConnection() returns to pool

void exercise8() {
    // ConnectionPool pool("localhost", 8888, 5);
    // auto conn = pool.getConnection();
    // conn.send("data");
    // pool.releaseConnection(conn);
    cout << "(Implement connection pool)" << endl;
}

// ============================================================
// MAIN
// ============================================================

int main() {
    cout << "=== Networking Exercises ===" << endl;
    
    cout << "\nEx1: HTTP GET" << endl;
    exercise1();
    
    cout << "\nEx2: Chat Server" << endl;
    exercise2();
    
    cout << "\nEx3: File Transfer" << endl;
    exercise3();
    
    cout << "\nEx4: UDP Ping" << endl;
    exercise4();
    
    cout << "\nEx5: Port Scanner" << endl;
    exercise5();
    
    cout << "\nEx6: DNS Lookup" << endl;
    exercise6();
    
    cout << "\nEx7: HTTP Server" << endl;
    exercise7();
    
    cout << "\nEx8: Connection Pool" << endl;
    exercise8();
    
    return 0;
}

// ============================================================
// ANSWERS (Partial)
// ============================================================
/*
Ex1:
#include <netdb.h>
string httpGet(const string& host, int port, const string& path) {
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    
    hostent* server = gethostbyname(host.c_str());
    sockaddr_in addr{};
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    memcpy(&addr.sin_addr.s_addr, server->h_addr, server->h_length);
    
    connect(sock, (sockaddr*)&addr, sizeof(addr));
    
    string request = "GET " + path + " HTTP/1.1\r\n"
                    "Host: " + host + "\r\n"
                    "Connection: close\r\n\r\n";
    send(sock, request.c_str(), request.length(), 0);
    
    string response;
    char buffer[1024];
    ssize_t bytes;
    while ((bytes = recv(sock, buffer, sizeof(buffer), 0)) > 0) {
        response.append(buffer, bytes);
    }
    
    close(sock);
    return response;
}

Ex6:
#include <netdb.h>
string resolve(const string& hostname) {
    addrinfo hints{}, *result;
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    
    if (getaddrinfo(hostname.c_str(), nullptr, &hints, &result) != 0) {
        return "";
    }
    
    char ip[INET_ADDRSTRLEN];
    sockaddr_in* addr = (sockaddr_in*)result->ai_addr;
    inet_ntop(AF_INET, &addr->sin_addr, ip, sizeof(ip));
    
    freeaddrinfo(result);
    return string(ip);
}

Ex5:
vector<int> scanPorts(const string& host, int start, int end) {
    vector<int> open;
    for (int port = start; port <= end; port++) {
        int sock = socket(AF_INET, SOCK_STREAM, 0);
        sockaddr_in addr{};
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        inet_pton(AF_INET, host.c_str(), &addr.sin_addr);
        
        // Set timeout
        timeval tv = {0, 100000};  // 100ms
        setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
        
        if (connect(sock, (sockaddr*)&addr, sizeof(addr)) == 0) {
            open.push_back(port);
        }
        close(sock);
    }
    return open;
}
*/
Exercises - C++ Tutorial | DeepML