Group B

Q8: Write a program using TCP socket for a wired network to perform the following tasks: 1) Say Hello to each other. 2) Transfer a file.

TCP Socket Programming - Hello and File Transfer

Solution and implementation for Q8 from Computer Networks (cnl).

8_tcp_py.txt Download
TCP Client-Server Communication (Message & File Transfer)
This program demonstrates TCP socket communication between a client and server to:

Files Required

server.py - TCP Server (receives messages/files)
client.py - TCP Client (sends messages/files)

--------------------------------------------------------------
server.py:

import socket
import os

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 8080))
server.listen(1)
print("Server Started")

os.makedirs("received_files", exist_ok=True)

while True:
    client, addr = server.accept()
    option = client.recv(10).decode().strip()
    
    if option == "MSG":
        message=client.recv(1024).decode()
        print(message)

    elif option == "FILE":
        filename = client.recv(256).decode().strip()

        with open(f"received_files/{filename}", "wb") as f:
            while (data := client.recv(4096)):
                f.write(data)
        print(f"Saved: {filename}\n")
    
    client.close()

--------------------------------------------------------------
client.py:

import socket
import os

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("127.0.0.1", 8080))  # Change IP for network

choice = input("(1) Message or (2) File? ")

if choice == "1":
    client.send(b"MSG")
    client.send(input("Enter message: ").encode())
    print("Sent!")
    
elif choice == "2":
    client.send(b"FILE")
    filepath = input("Enter file path: ")
    client.send(os.path.basename(filepath).encode())
    
    with open(filepath, "rb") as f:
        while (chunk := f.read(4096)):
            client.send(chunk)
    print("File sent!")

client.close()

-------------------------------------------------------------
How to Run
Terminal 1 (Start Server):
python server.py
Terminal 2 (Run Client):
python client.py
Then:

Enter 1 to send a message (Hello)
Enter 2 to transfer a file

Note: Change 127.0.0.1 to the actual server IP address in client.py for network communication.
8_tcp_hello.txt Download
8) TCP Client-Server Communication (Text & File Transfer)

Files:
- server.cpp (TCP Server)
- client.cpp (TCP Client)

## SERVER (server.cpp):

#include <iostream>
#include <fstream>
#include <winsock2.h>

#pragma comment(lib, "ws2_32.lib") // Winsock library

int main() {
    WSADATA wsa;
    WSAStartup(MAKEWORD(2, 2), &wsa);

    SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);

    sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = INADDR_ANY;
    serverAddr.sin_port = htons(8080);

    bind(serverSocket, (sockaddr*)&serverAddr, sizeof(serverAddr));
    listen(serverSocket, 5);

    std::cout << "Waiting for client...\n";
    SOCKET clientSocket = accept(serverSocket, nullptr, nullptr);

    char option[10] = {0};
    recv(clientSocket, option, sizeof(option), 0);

    if (strcmp(option, "MSG") == 0) {
        char buffer[1024] = {0};
        recv(clientSocket, buffer, sizeof(buffer), 0);
        std::cout << "Message from client: " << buffer << std::endl;
    } else if (strcmp(option, "FILE") == 0) {
        char filename[256] = {0};
        recv(clientSocket, filename, sizeof(filename), 0);

        std::ofstream outFile(filename, std::ios::binary);
        char buffer[1024];
        int bytesReceived;

        while ((bytesReceived = recv(clientSocket, buffer, sizeof(buffer), 0)) > 0) {
            outFile.write(buffer, bytesReceived);
            if (bytesReceived < sizeof(buffer)) break;
        }

        outFile.close();
        std::cout << "File received: " << filename << std::endl;
    }

    closesocket(clientSocket);
    closesocket(serverSocket);
    WSACleanup();
    return 0;
}

Terminal Commands:
1) g++ server.cpp -o server -lws2_32
2) .\server.exe

## CLIENT (client.cpp):

#include <iostream>
#include <fstream>
#include <winsock2.h>

#pragma comment(lib, "ws2_32.lib") // Winsock library

int main() {
    WSADATA wsa;
    WSAStartup(MAKEWORD(2, 2), &wsa);

    SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);

    sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serverAddr.sin_port = htons(8080);

    connect(clientSocket, (sockaddr*)&serverAddr, sizeof(serverAddr));

    int choice;
    std::cout << "Send (1) Message or (2) File? ";
    std::cin >> choice;
    std::cin.ignore();

    if (choice == 1) {
        const char* option = "MSG";
        send(clientSocket, option, strlen(option), 0);

        std::string message;
        std::cout << "Enter message: ";
        std::getline(std::cin, message);
        send(clientSocket, message.c_str(), message.size(), 0);
    } else if (choice == 2) {
        const char* option = "FILE";
        send(clientSocket, option, strlen(option), 0);

        std::string filepath;
        std::cout << "Enter file path: ";
        std::getline(std::cin, filepath);

        std::string filename = filepath.substr(filepath.find_last_of("\\/") + 1);
        send(clientSocket, filename.c_str(), filename.size(), 0);

        std::ifstream inFile(filepath, std::ios::binary);
        char buffer[1024];
        while (!inFile.eof()) {
            inFile.read(buffer, sizeof(buffer));
            send(clientSocket, buffer, inFile.gcount(), 0);
        }

        inFile.close();
        std::cout << "File sent: " << filename << std::endl;
    }

    closesocket(clientSocket);
    WSACleanup();
    return 0;
}

Terminal Commands:
1) g++ client.cpp -o client -lws2_32
2) .\client.exe

Other Questions in Computer Networks

See All Available Questions
Download