Home / Expert Answers / Computer Science / server-g-o-tcpserver-tcpserver-cpp-tcpserver-server-include-lt-stdio-h-gt-include-pa313

(Solved): Server: // g++ -o tcpServer tcpServer.cpp // ./tcpServer Server #include <stdio.h> #include & ...



student submitted image, transcription available below

Server:

// g++ -o tcpServer tcpServer.cpp

// ./tcpServer Server

#include <stdio.h>

#include <netdb.h>

#include <netinet/in.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#include <sys/types.h>

#define MAX 80

#define PORT 54550

#define SA struct sockaddr

// Function designed for chat between client and server.

void openChat(int sockfd, char *name)

{

    char buff[MAX], clientName[MAX];

    int n;

   

    // infinite loop for chat

    for (;;) {

        bzero(buff, MAX);

        // read the message from client and copy it in buffer

        read(sockfd, buff, sizeof(buff));

        // print buffer which contains the client contents

        printf("From client: %s \n %s: ", buff, name);

        bzero(buff, MAX);

        n = 0;

        // copy server message in the buffer

        while ((buff[n++] = getchar()) != '\n')

            ;

        // and send that buffer to client

        write(sockfd, buff, sizeof(buff));

    printf("Message sent. Waiting for client\n");

        // if msg contains "Exit" then server exit and chat ended.

        if (strncmp("exit", buff, 4) == 0) {

            printf("Server Exit...\n");

            break;

        }

    }

}

// Driver function

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

{

    int sockfd, connfd, len;

    struct sockaddr_in servaddr, cli;

    socklen_t addr_size;

   

    char *name = argv[1];

    // socket create and verification

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd == -1) {

        printf("socket creation failed...\n");

        exit(0);

    }

    else

        printf("Socket successfully created..\n");

    bzero(&servaddr, sizeof(servaddr));

    // assign IP, PORT

    servaddr.sin_family = AF_INET;

    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    servaddr.sin_port = htons(PORT);

    // Binding newly created socket to given IP and verification

    if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {

        printf("socket bind failed...\n");

        exit(0);

    }

    else

        printf("Socket successfully binded..\n");

    // Now server is ready to listen and verification

    if ((listen(sockfd, 5)) != 0) {

        printf("Listen failed...\n");

        exit(0);

    }

    else

        printf("Server listening..\n");

    len = sizeof(cli);

    // Accept the data packet from client and verification

    connfd = accept(sockfd, (SA*)&cli, &addr_size);

    if (connfd < 0) {

        printf("server acccept failed...\n");

        exit(0);

    }

    else

        printf("server acccepted the client...\n");

    // Open chat

    openChat(connfd, name);

    // After chatting close the socket

    close(sockfd);

}

Client:

// g++ -o tcpClient tcpClient.cpp

// ./tcpClient Client

#include <netdb.h>

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#define MAX 80

#define PORT 54550

#define SA struct sockaddr

void openChat(int sockfd, char *name)

{

    char buff[MAX], serverName[MAX];

    int n;

   

   

    for (;;) {

        bzero(buff, sizeof(buff));

        printf("%s : ", name);

        n = 0;

        while ((buff[n++] = getchar()) != '\n')

            ;

        write(sockfd, buff, sizeof(buff));

        printf("Message sent. Waiting for server\n");

        bzero(buff, sizeof(buff));

        read(sockfd, buff, sizeof(buff));

        printf("From Server : %s", buff);

        if ((strncmp(buff, "exit", 4)) == 0) {

            printf("Client Exit...\n");

            break;

        }

    }

}

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

{

    int sockfd, connfd;

    struct sockaddr_in servaddr, cli;

    char *name = argv[1];

    // socket create and varification

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd == -1) {

        printf("socket creation failed...\n");

        exit(0);

    }

    else

        printf("Socket successfully created..\n");

    bzero(&servaddr, sizeof(servaddr));

    // assign IP, PORT

    servaddr.sin_family = AF_INET;

    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    servaddr.sin_port = htons(PORT);

    // Client to server socket connection

    if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {

        printf("Could not connect with server...\n");

        exit(0);

    }

    else

        printf("connected to the server..\n");

    // open chat

    openChat(sockfd, name);

    // close socket

    close(sockfd);

}

4. (4\%) Socket programming multiple client single server implementation We covered an example of socket programming in week 8 where we implemented and tested a chat application between a single client and a single server. The code examples for both the client and server are available via Blackboard (Week 8 of Programming discussions section). Requirement: Your task is to extend TCP server file and allow multiple client connections (and chats). At present, the ports are hardcoded in the programs. For ease, you can make port input as part of the command line arguments for the clients so that you can avoid hard coding port numbers for each newly created clients. At the minimum, two clients should be able to connect with the server (you can test more than 2 for robustness). |


We have an Answer from Expert

View Expert Answer

Expert Answer



We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe