0

I am needing to edit this client/server echo program into a client/server talk application, to send messages back and forth between the client and server, rather than have the client echo the message only. So the output would be:
C: Hello
S: Hello
S: How are you?
C: How are you?

I am not understand really what to edit, where, why, etc.
I know I would understand it if I saw it, but any help at all would be great.
Thank you so much!

tcpserv01.c

#include    "unp.h"

int
main(int argc, char **argv)
{
    int                    listenfd, connfd;
    pid_t                childpid;
    socklen_t            clilen;
    struct sockaddr_in    cliaddr, servaddr;

    listenfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family      = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port        = htons(SERV_PORT);

    bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

    listen(listenfd, LISTENQ);

    for ( ; ; ) {
        clilen = sizeof(cliaddr);
        connfd = accept(listenfd, (SA *) &cliaddr, &clilen);

        if ( (childpid = fork()) == 0) {    /* child process */
            close(listenfd);    /* close listening socket */
            str_echo(connfd);    /* process the request */
            exit(0);
        }
        close(connfd);            /* parent closes connected socket */
    }
}

tcpcli01.c

#include    "unp.h"

int
main(int argc, char **argv)
{
    int                    sockfd;
    struct sockaddr_in    servaddr;

    if (argc != 2)
        err_quit("usage: tcpcli <IPaddress>");

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERV_PORT);
    inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

    connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

    str_cli(stdin, sockfd);        /* do it all */

    exit(0);
}

str_echo.c

#include    "unp.h"

void
str_echo(int sockfd)
{
    ssize_t        n;
    char        buf[MAXLINE];


    while ( (n = read(sockfd, buf, MAXLINE)) > 0)
        write(sockfd, buf, n);
}

str_cli.c

#include    "unp.h"

void
str_cli(FILE *fp, int sockfd)
{
    char    sendline[MAXLINE], recvline[MAXLINE];

    while (fgets(sendline, MAXLINE, fp) != NULL) {

        write(sockfd, sendline, strlen(sendline));

        if (read(sockfd, recvline, MAXLINE) == 0)
            err_quit("str_cli: server terminated prematurely");

        fputs(recvline, stdout);
    }
}
2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by ChainedHollow
0

This brings up one question. Where is the server getting the responds message? Is it from some canned messages that it reads down into?

0

Thank you. The messages are to be read from the user on each side, to be sent to the client or server and displayed.
So I if I type Hello on the client, the server will display Hello. etc.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.