SOURCE FILE: chatserver.c
/* chatserver.c */
#include <stdlib.h>
#include <stdio.h>
#include <cnaiapi.h>
#define BUFFSIZE 256
#define INPUT_PROMPT "Input > "
#define RECEIVED_PROMPT "Received> "
int recvln(connection, char *, int); /* READ A LINE FROM A SOCKET */
int readln(char *, int); /* READ A LINE FROM STANDARD INPUT */
/*-----------------------------------------------------------------------
*
* Program: chatserver
* Purpose: wait for a connection from a chatclient & allow users to chat
* Usage: chatserver <appnum>
*
*-----------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
connection conn;
int len;
char buff[BUFFSIZE];
if (argc != 2) {/* 'ERROR OUT' IF #ARGS WRONG */
(void) fprintf(stderr, "usage: %s <appnum>\n", argv[0]);
exit(1);
}
/* PRINT MESSAGE ON SCREEN */
(void) printf("Chat Server Waiting For Connection.\n");
/* wait for a connection from a chatclient */
conn = await_contact((appnum) atoi(argv[1]));
if (conn < 0)
exit(1);
/* PRINT MESSAGE ON SCREEN */
(void) printf("Chat Connection Established.\n");
/* iterate, reading from the client and the local user */
/* EXPRESSION IN 'while' IS TRUE IF len > 0 */
while((len = recvln(conn, buff, BUFFSIZE)) > 0)
{
/* THE NEXT FEW LINES OF CODE WRITE THE RECEIVED LINE AND
PROMPT FOR A REPLY */
(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);
/* READ THE LINE FROM THE 'SERVER USER'
AND BREAK IF THERE ARE ZERO CHARACTERS
(JUMP OUTSIDE THE LOOP) */
if ((len = readln(buff, BUFFSIZE)) < 1)
break;
buff[len - 1] = '\n';
/* SEND THE LINE TO THE CLIENT */
(void) send(conn, buff, len, 0);
} /* END OF WHILE-LOOP
/* iteration ends when EOF found on stdin or chat connection */
(void) send_eof(conn);
(void) printf("\nChat Connection Closed.\n\n");
return 0;
}