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

COSC 1430: HomeworK S-Communication To be turned in on the server at 11:59 PM, T

ID: 3734561 • Letter: C

Question

COSC 1430: HomeworK S-Communication To be turned in on the server at 11:59 PM, Tuesday March 27 1- Objective The purpose of this assignment is for students to gain a better understanding of C++ inheritance and polymorphism mechanics 2- Administrative Details All files related to this homework should be submitted on the server in your home directory. If files are not submitted on the server, they will not be graded. Your code will be compiled and tested on the server so make sure you have thoroughly tested it on the server and not your local IDE. It's recommended to include a README.txt file that includes details about your program, such as how to compile. 3- Program Description This homework will simulate encrypted communication between two clients, who are connected via a server There will be four classes required in total This means you will need to turn in 9 files -server.h, server.cpp, client.h, client.cpp, messageServer.h, messageServer.cpp, encodedServer.h, encodedServer.cpp, main.cpp. The functions that are include here are the absolutely necessary functions. You can add additional functions if you need to in order to fulfill requirements. That is, a server function that returns the id of the sender or the d of the receiver On the right, a graphical representation to see how the four dlasses will interact.

Explanation / Answer

//server program

//header files inclusion

#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
    int serverfd, socketnew, readval;
    struct sockaddr_in address;
    int opt = 1;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};
    char *hello = "Hello from server";
    if ((serverfd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
    {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }
    if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
                                                  &opt, sizeof(opt)))
    {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons( PORT );
    if (bind(serverfd, (struct sockaddr *)&address,
                                 sizeof(address))<0)
    {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }
    if (listen(serverfd, 3) < 0)
    {
        perror("listen");
        exit(EXIT_FAILURE);
    }
    if ((socketnew = accept(serverfd, (struct sockaddr *)&address,
                       (socklen_t*)&addrlen))<0)
    {
        perror("accept");
        exit(EXIT_FAILURE);
    }
    readval = read( socketnew , buffer, 1024);
    printf("%s ",buffer );
    send(socketnew , hello , strlen(hello) , 0 );
    printf("Hello message sent ");
    return 0;
}

Client

//header files inclusion

#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
//main function starts here
int main(int argc, char const *argv[])
{
    struct sockaddr_in address;
    int sock = 0, valread;
    struct sockaddr_in serv_addr;
    char *hello = "Hello from client";
    char buff[1024] = {0};
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf(" Socket creation error ");
        return -1;
    }

    memset(&serv_addr, '0', sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);
    
    // Convert IPv4 and IPv6 addresses from text to binary form
    if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
    {
        printf(" Invalid address/ Address not supported ");
        return -1;
    }

    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf(" Connection Failed ");
        return -1;
    }
    send(sock , hello , strlen(hello) , 0 );
    printf("Hello message sent ");
    valread = read( sock , buff, 1024);
    printf("%s ",buff );
    return 0;
}