0

ok so i am having problems with my program. its a client program talking to the server program. i have both programs working fine but i do not know how to get the conversation to display on the server side of the conversation. here are the codes and makefile

serv.c


#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>

#define MAXLINE 4096
#define SA struct sockaddr
#define SERV_PORT 9877
#define LISTENQ 1024
#define EINTR 0

void str_echo(int sockfd);

int main(int argc,char **argv)
{       int listenfd,connfd,errno;
        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)
                {       close(listenfd);
                        str_echo(connfd);
                        exit(0);
                }
                close(connfd);
        }
}

void str_echo(int sockfd)
{       ssize_t n, errno;
        char rec_buf[MAXLINE], send_buf[MAXLINE];

        again:
                while((n=read(sockfd,rec_buf,MAXLINE))>0)
                {       fputs(rec_buf,stdout);
                        fgets(send_buf,MAXLINE,stdin);
                        write(sockfd,send_buf,MAXLINE);

                        if(n<0 && errno==EINTR)
                        {       goto again;
                        }else if(n<0)
                        {       fprintf(stderr,"str_echo: read error");
                                exit(1);
                        }
                }
}



cli.c


#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>

#define SA struct sockaddr
#define SERV_PORT 9877
#define MAXLINE 4096

void str_cli(FILE *fp, int sockfd);

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

        if(argc !=2)
        {       fprintf(stderr,"usage:tcpcli <IPaddress>");
                exit(1);
        }

        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);

        exit(0);
}

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)
                {       fprintf(stderr,"str_cli: server terminated");
                        exit(1);
                }
                fputs(recvline,stdout);
        }
}


and the makefile

all: client server

client: cli.c
        cc -o client cli.c

server: serv.c
        cc -o server serv.c

clean:
        rm client server
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by ROTC89
0

i figured it out. i just took out the while loop in the server str_echo but left the data that was in the while loop and set the MAXLINE to MAXLINE -1 and it works. i also took out both the buffers and just had one called buff.

Edited by ROTC89: n/a

This question has already been answered. 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.