Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your second homework focuses on TCP/IP and network programming. Please follow th

ID: 3704573 • Letter: Y

Question

Your second homework focuses on TCP/IP and network programming. Please follow the lectures to write programs using CIC++ 1. (a) A host in an organization has an IP address 150.32.64.34 and a subnet mask 255.255.240.0. What is the address of this subnet? What is the range of IP addresses that a host can have on this subnet? (b) A university has 150 LANs with 100 hosts in each LAN. Suppose the university has one Class B address. Design an appropriate subnet addressing scheme. 2. Using C/C++ and sock programming, write both the client and server programs to implement a task. You can choose the task by yourself (e.g. a chatting application between the server and the client, or a file transfer application between the server and the client, etc.) In the hardcopy, please write clearly what your server program and client programs do.

Explanation / Answer

ANSWER FOR a:

The following mask operation derives the subnet address,

Address : 10010110 00100000 01000000 00100010

Mask : 11111111 11111111 11110000 00000000

Subnet : 10010110 00100000 01000000 00000000

The range of IP addresses that a host can have is as follows,

From : 10010110 00100000 01000000 00000000

To : 10010110 00100000 01001111 11111111

ANSWER FOR b:

A Class B address has 14 bits for the network ID and 16 bits for the host ID. To design an appropriate subnet addressing scheme we need to decide how many bits to allocate to the host ID versus the subnet ID. We can choose either 7 bits or 8 bits to identify the hosts. If we allocate 8 bits for to identify the host, as shown below, then there are sufficient subnet-id bits to cover up to 28 =256 LANs and enough host-id bits to cover up to 256 hosts for each LAN. The subnet mask in this case is 255.255.255.0 .

If we allocate 7 bits for to identify the host, as shown below, then there are sufficient subnet-id bits to cover up to 29 =512 LANs and enough host-id bits to cover up to 128 hosts for each LAN. The subnet mask in this case is 255.255.255.128.

2)

#include <iostream>

#include <string>

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <netdb.h>

#include <sys/uio.h>

#include <sys/time.h>

#include <sys/wait.h>

#include <fcntl.h>

#include <fstream>

using namespace std;

int main(int argc, char *argv[])

{

    if(argc != 2)

    {

        cerr << "Usage: port" << endl;

        exit(0);

    }

    int port = atoi(argv[1]);

    char msg[1500];

    sockaddr_in servAddr;

    bzero((char*)&servAddr, sizeof(servAddr));

    servAddr.sin_family = AF_INET;

    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);

    servAddr.sin_port = htons(port);

    int serverSd = socket(AF_INET, SOCK_STREAM, 0);

    if(serverSd < 0)

    {

        cerr << "Error establishing the server socket" << endl;

        exit(0);

    }

    int bindStatus = bind(serverSd, (struct sockaddr*) &servAddr,

        sizeof(servAddr));

    if(bindStatus < 0)

    {

        cerr << "Error binding socket to local address" << endl;

        exit(0);

  }

    cout << "Waiting for a client to connect..." << endl;

    listen(serverSd, 5);

    sockaddr_in newSockAddr;

    socklen_t newSockAddrSize = sizeof(newSockAddr);

    int newSd = accept(serverSd, (sockaddr *)&newSockAddr, &newSockAddrSize);

    if(newSd < 0)

    {

        cerr << "Error accepting request from client!" << endl;

        exit(1);

    }

    cout << "Connected with client!" << endl;

    struct timeval start1, end1;

    gettimeofday(&start1, NULL);

    int bytesRead, bytesWritten = 0;

    while(1)

    {

        cout << "Awaiting client response..." << endl;

        memset(&msg, 0, sizeof(msg));//clear the buffer

        bytesRead += recv(newSd, (char*)&msg, sizeof(msg), 0);

        if(!strcmp(msg, "exit"))

        {

            cout << "Client has quit the session" << endl;

            break;

        }

        cout << "Client: " << msg << endl;

        cout << ">";

        string data;

        getline(cin, data);

        memset(&msg, 0, sizeof(msg)); //clear the buffer

        strcpy(msg, data.c_str());

        if(data == "exit")

        {

            send(newSd, (char*)&msg, strlen(msg), 0);

            break;

        }

        bytesWritten += send(newSd, (char*)&msg, strlen(msg), 0);

    }

    gettimeofday(&end1, NULL);

    close(newSd);

    close(serverSd);

    cout << "********Session********" << endl;

    cout << "Bytes written: " << bytesWritten << " Bytes read: " << bytesRead << endl;

    cout << "Elapsed time: " << (end1.tv_sec - start1.tv_sec)

        << " secs" << endl;

    cout << "Connection closed..." << endl;

    return 0;

}

This is for Client Side

#include <iostream>

#include <string>

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <netdb.h>

#include <sys/uio.h>

#include <sys/time.h>

#include <sys/wait.h>

#include <fcntl.h>

#include <fstream>

using namespace std;

int main(int argc, char *argv[])

{

    if(argc != 3)

    {

        cerr << "Usage: ip_address port" << endl; exit(0);

    }

    char *serverIp = argv[1]; int port = atoi(argv[2]);

    char msg[1500];

    struct hostent* host = gethostbyname(serverIp);

    sockaddr_in sendSockAddr;

    bzero((char*)&sendSockAddr, sizeof(sendSockAddr));

    sendSockAddr.sin_family = AF_INET;

    sendSockAddr.sin_addr.s_addr =

        inet_addr(inet_ntoa(*(struct in_addr*)*host->h_addr_list));

    sendSockAddr.sin_port = htons(port);

    int clientSd = socket(AF_INET, SOCK_STREAM, 0);

    int status = connect(clientSd,

                         (sockaddr*) &sendSockAddr, sizeof(sendSockAddr));

    if(status < 0)

    {

        cout<<"Error connecting to socket!"<<endl; break;

    }

    cout << "Connected to the server!" << endl;

    int bytesRead, bytesWritten = 0;

    struct timeval start1, end1;

    gettimeofday(&start1, NULL);

    while(1)

    {

        cout << ">";

        string data;

        getline(cin, data);

       memset(&msg, 0, sizeof(msg));//clear the buffer

        strcpy(msg, data.c_str());

        if(data == "exit")

        {

            send(clientSd, (char*)&msg, strlen(msg), 0);

            break;

        }

        bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0);

        cout << "Awaiting server response..." << endl;

        memset(&msg, 0, sizeof(msg));//clear the buffer

        bytesRead += recv(clientSd, (char*)&msg, sizeof(msg), 0);

        if(!strcmp(msg, "exit"))

        {

            cout << "Server has quit the session" << endl;

            break;

        }

        cout << "Server: " << msg << endl;

    }

    gettimeofday(&end1, NULL);

    close(clientSd);

    cout << "********Session********" << endl;

    cout << "Bytes written: " << bytesWritten <<

    " Bytes read: " << bytesRead << endl;

    cout << "Elapsed time: " << (end1.tv_sec- start1.tv_sec)

      << " secs" << endl;

    cout << "Connection closed" << endl;

    return 0;  

}