Question: Introduction : In this assignment you will write a simple chat client and server as an exercise in network programming. The chat server must be
Introduction: In this assignment you will write a simple chat client and server as an exercise in network programming. The chat server must be able to support multiple clients. Each client has a name, which is the first thing that they send to the server. The server maintains a short history of the recent conversation, the length of this history is12 lines. When a new client enters the conversation, the history is sent to them so they have some context for the conversation. The client program accepts a line of input from the user and sends the line to the server. The client also receives lines from the server. Each line starts with the name of the user who entered the line followed by a colon.
Details: Any client should be able to converse with any server, so the interface between them must be well defined. The lib.c library from class must be used to send text strings between the programs. The user name is the first and only parameter to the client program. The first string that the client sends to the server is the name of the user. Subsequent strings are the lines that the user types at the terminal. When the user leaves the conversation, the socket is closed and the server removes any information that they have stored for the user. The server listens on port 55555 for connections from clients. It must be able to handle multiple clients so they can have a conversation. For the server implementation your can use a separate child process for each client, a separate thread for each client, or use the select() based architecture.
/******************************************************** * * client.c * * This is the starting point for your char client. *******************************************************/
#include #include #include #include #include #include #include #include #include "lib.h"
int main(int argc, char **argv) { struct addrinfo hints; struct addrinfo *addr; struct sockaddr_in *addrinfo; int rc; int sock;
}
/*********************************************** * * server.c * * This is the starting point for your chat server **********************************************/
#include #include #include #include #include #include #include #include #include "lib.h"
int main(int argc, char **argv) { int sock, conn; int i; int rc; struct sockaddr address; socklen_t addrLength = sizeof(address); struct addrinfo hints; struct addrinfo *addr;
}
/****************************************** * * lib.c * * Library of procedures that are useful * for reliable network programming *****************************************/
#include #include #include #include #include #include #include "lib.h"
/* * Read count bytes from fd if at all possible. * The only potential problem is an end of file * which suggests a problem with the server. */ int readn(int fd, char *buffer, int count) { char *ptr; int n; int left;
ptr = buffer; left = count; while(left > 0) { n = read(fd, ptr, left); if(n == 0) break; if(n < 0) { if(errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { n = 0; } else { return(-1); } } left -= n; ptr += n; } return(count-left); }
/* * Write count bytes on fd if at all possible. * The only potential problem is an error that * can't be recovered from. */ int writen(int fd, char *buffer, int count) { char *ptr; int n; int left;
ptr = buffer; left = count; while(left > 0) { n = write(fd, ptr, left); if(n < 0) { if(errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { n = 0; } else { return(-1); } } left -= n; ptr += n; } return(count-left); }
/* * Reliably read a text string from fd. * Returns NULL if it can't read the string * otherwise a pointer to the string. */ char *readString(int fd) { short len; char *buffer; int ret;
ret = readn(fd, (char*)&len, sizeof(len)); if(ret <= 0) return(NULL); len = ntohs(len); buffer = (char*) malloc(len); ret = readn(fd, buffer, len); if(ret != len) return(NULL); else return(buffer); }
/* * Reliably write the string on fd. * Returns -1 on failure, 0 on success. */ int writeString(int fd, char *string) { short len; int ret; short buffer;
len = strlen(string)+1; buffer = htons(len); ret = writen(fd, (char*)&buffer, sizeof(len)); if(ret != sizeof(len)) return(-1); ret = writen(fd, string, len); if(ret != len) return(-1); else return(0); }
/************************************* * * lib.h * * Declarations of the procedures in our * library of useful procedures for * reliable network programs. *************************************/
int readn(int fd, char *buffer, int count); int writen(int fd, char *buffer, int count); char *readString(int fd); int writeString(int fd, char *string);
MakeFile
.PHONY : all clean CFLAGS = -Wall -g
all : server client
server : server.o lib.o cc -o server server.o lib.o
client : client.o lib.o cc -o client client.o lib.o
lib.o : lib.h
server.o : lib.h
client.o : lib.h
clean : rm client server client.o server.o lib.o
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
