Question: Modify the C Program Chat-Client Socket Server Program to include a login protocol. This is a program that is launched through windows command line for
Modify the C Program Chat-Client Socket Server Program to include a login protocol.
This is a program that is launched through windows command line for a simple chat to client chat. The code is given for us to modify to include a login protocol. The login protocol is this: the client enters a Username, the server then proceeds to look through a .text file to see if the user name exits. If the username exits it will then ask the client for the password. The client enters the password. The server then checks if it matches with the password on the .text file.
As it stands now the program lets the client immediately chat with the server the moment a connection is established. You can hardcode the username/password into the code if it's easier, i am having trouble having the server send automatic messages back to the client.
chatClient.c
/* chatclient.c */
#include
#include
#include
#define BUFFSIZE 256
#define INPUT_PROMPT "Input > "
#define RECEIVED_PROMPT "Received> "
int recvln(connection, char *, int);
int readln(char *, int);
/*-----------------------------------------------------------------------
*
* Program: chatclient
* Purpose: contact a chatserver and allow users to chat
* Usage: chatclient
*
*-----------------------------------------------------------------------
*/
int
main(int argc, char *argv[])
{
computer comp;
connection conn;
char buff[BUFFSIZE];
int len;
if (argc != 3) {
(void) fprintf(stderr, "usage: %s
argv[0]);
exit(1);
}
/* convert the compname to binary form comp */
comp = cname_to_comp(argv[1]);
if (comp == -1)
exit(1);
/* make a connection to the chatserver */
conn = make_contact(comp, (appnum) atoi(argv[2]));
if (conn < 0)
exit(1);
(void) printf("Chat Connection Established. ");
(void) printf(INPUT_PROMPT);
(void) fflush(stdout);
/* iterate, reading from local user and then from chatserver */
while((len = readln(buff, BUFFSIZE)) > 0) {
buff[len - 1] = ' ';
(void) send(conn, buff, len, 0);
/* receive and print a line from the chatserver */
if ((len = recvln(conn, buff, BUFFSIZE)) < 1)
break;
(void) printf(RECEIVED_PROMPT);
(void) fflush(stdout);
(void) write(STDOUT_FILENO, buff, len);
(void) printf(INPUT_PROMPT);
(void) fflush(stdout);
}
/* iteration ends when stdin or the connection indicates EOF */
(void) printf(" Chat Connection Closed. ");
(void) send_eof(conn);
exit(0);
}
chatServer.c
/* chatserver.c */
#include
#include
#include
#define BUFFSIZE 256
#define INPUT_PROMPT "Input > "
#define RECEIVED_PROMPT "Received> "
int recvln(connection, char *, int);
int readln(char *, int);
/*-----------------------------------------------------------------------
*
* Program: chatserver
* Purpose: wait for a connection from a chatclient & allow users to chat
* Usage: chatserver
*
*-----------------------------------------------------------------------
*/
int
main(int argc, char *argv[])
{
connection conn;
int len;
char buff[BUFFSIZE];
if (argc != 2) {
(void) fprintf(stderr, "usage: %s
exit(1);
}
(void) printf("Chat Server Waiting For Connection. ");
/* wait for a connection from a chatclient */
conn = await_contact((appnum) atoi(argv[1]));
if (conn < 0)
exit(1);
(void) printf("Chat Connection Established. ");
/* iterate, reading from the client and the local user */
while((len = recvln(conn, buff, BUFFSIZE)) > 0) {
(void) printf(RECEIVED_PROMPT);
(void) fflush(stdout);
(void) write(STDOUT_FILENO, buff, len);
/* send a line to the chatclient */
(void) printf(INPUT_PROMPT);
(void) fflush(stdout);
if ((len = readln(buff, BUFFSIZE)) < 1)
break;
buff[len - 1] = ' ';
(void) send(conn, buff, len, 0);
}
/* iteration ends when EOF found on stdin or chat connection */
(void) send_eof(conn);
(void) printf(" Chat Connection Closed. ");
return 0;
}
cnaiapi.h (Header file for both programs)
#ifndef _CNAIAPI_H_
#define _CNAIAPI_H_
#if defined(LINUX) || defined(SOLARIS)
#include
#include
#include
#include
#include
#endif /* defined(LINUX) || defined(SOLARIS) */
#if defined(_WIN32)
#include
#include
#endif /* defined(_WIN32) */
#include
typedef short appnum;
typedef long computer;
typedef int connection;
struct port2sock {
short port;
int sock;
};
#define P2S_SIZE 64 /* number of entries in port to socket map table */
#define LISTEN_Q_LEN 5
appnum appname_to_appnum(char *appname);
computer cname_to_comp(char *cname);
connection await_contact(appnum a);
connection make_contact(computer c, appnum a);
int send_eof(connection c);
void cnaiapi_init(void);
#if defined(LINUX) || defined(SOLARIS)
extern pthread_mutex_t await_contact_mutex, cname_mutex, appname_mutex;
#elif defined(_WIN32)
extern HANDLE await_contact_mutex, cname_mutex, appname_mutex;
#endif
#endif /* !defined(_CNAIAPI_H_) */
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
