Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Putty/Linux C Language Modify the files below so it become a two-way chat that allows both client and server can read and write text from/to

Putty/Linux C Language

Modify the files below so it become a two-way chat that allows both client and server can read and write text from/to each other. Server3.c only send messages and client3.c only receives at the moment.

---------------------------------------------------------------------------------------------------------------------

server3.c

#include #include #include #include #include #include #include #include #include #include /* inet_aton */ #define MYPORT 6996 /* the port the client will be connecting to */ #define BACKLOG 10 /* Needed for "listen" in case many machines try to connect this cuts it off at 10. */ #define MAXDATASIZE 100 main() { int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */ struct sockaddr_in my_addr; /* my address information */ struct sockaddr_in their_addr; /* connector's address information */ int sin_size; char yer_text[MAXDATASIZE]="HEY HEY HEY!!! "; /* String that is sent from server to client */ printf("Starting up server program. "); printf("Enter strings to send to the client program or '/Q' to quit. "); if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) /* Create socket file descriptor */ { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; /* host byte order */ my_addr.sin_port = htons(MYPORT); /* short, network byte order */ my_addr.sin_addr.s_addr = INADDR_ANY; /* automatically fill with my IP */ bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */ if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) /* Bind socket to specific address */ { perror("bind"); exit(1); } printf("Waiting for connection from remote machine. "); if (listen(sockfd, BACKLOG) == -1) /* Wait for connection */ { perror("listen"); exit(1); } sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd, (struct sockaddr *) &their_addr,(socklen_t *) &sin_size)) == -1) /* Accept connection, make new file descriptor to write to */ { perror("accept"); } printf("server: got connection from %s ",inet_ntoa(their_addr.sin_addr)); while(scanf("%s", &yer_text)) /*get text from user*/ { if (send(new_fd, yer_text, strlen(yer_text), 0) == -1) /* Send the message to the client */ perror("send"); if(yer_text[0]=='/' && yer_text[1]=='Q') /* If the server types in /Q the program exits */ { printf(" GOODBYE!!! "); close(new_fd); exit(0); } } close(new_fd); /* Close up the newly created FD */ while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up all child processes */ } 

---------------------------------------------------------------------------------------------------------------------

client3.c

#include #include #include #include #include #include #include #include #include /* for close() */ #define PORT 13109 /* the port the client will be connecting to */ #define MAXDATASIZE 100 /* max number of bytes the message recieved can have */ int main(int argc, char *argv[]) { int sockfd, numbytes, a, count=0; char buf[MAXDATASIZE]; /* String into which recieved messages will go */ struct hostent *he; struct sockaddr_in their_addr; /* connector's address information */ printf("Starting client program. " ); if (argc != 2) /* Check command line */ { fprintf(stderr,"You forgot to enter the hostname of the server. usage: client3 hostname "); exit(1); } if ((he=gethostbyname(argv[1])) == NULL) /* get the host info */ { perror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) /* Create socket file descriptor */ { perror("socket"); exit(1); } their_addr.sin_family = AF_INET; /* host byte order */ their_addr.sin_port = htons(PORT); /* short, network byte order */ their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero), 8); /* zero the rest of the struct */ if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) /* Connect to server */ { perror("connect"); exit(1); } printf("Waiting for messages from the server. "); while(sockfd) /* While the file descriptor exists */ { count=count+1; /* Increment counter (to show message number) */ if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) /* Recieve data from server */ { perror("recv"); exit(1); } buf[numbytes] = '\0'; if(buf[0]=='/' && buf[1]=='Q') /* Check for quit */ { printf(" SERVER IS CALLING IT QUITS: GOODBYE "); exit(0); } printf("%d. %s ", count, buf); /* Print message */ } close(sockfd); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Be familiar with the basic ways to manage capacity.

Answered: 1 week ago

Question

Be familiar with the five basic ways to manage demand.

Answered: 1 week ago