// server.cpp #include "includes.h" #include "internet_server.h" void main(void) { char *line; char temp[100]; int feelingok=1; internet_server IS; if (!IS.isOK()) exit(1); while (1) { IS.waitforuser(); while (1) { line=IS.readlineLC(); if (strcmp(line,"**end**")==0) { printf("[session %d connection to %s lost]\n", IS.getSessionNumber(), IS.getClientName()); exit(0); } else if (strcmp(line,"hello")==0) { sprintf(temp,"Hello to you too, Mr. %s, this is session %d", IS.getClientName(), IS.getSessionNumber()); IS.writeline(temp); continue; } else if (strcmp(line,"what time is it?")==0) { time_t now; now=time(NULL); strcpy(temp,"It is "); strcat(temp,ctime(&now)); temp[strlen(temp)-1]=0; IS.writeline(temp); continue; } else if (strcmp(line,"how are you?")==0) { if (feelingok) IS.writeline("I'm OK thank you"); else IS.writeline("I'm not feeling at all well, but thank you for asking"); continue; } else if (strcmp(line,"drop dead")==0) { int parentid; if (feelingok) { parentid=getppid(); kill(parentid,SIGINT); feelingok=0; IS.writeline("OK, I have killed the server"); } else IS.writeline("I already killed the server"); continue; } else if (strcmp(line,"goodbye")==0) { IS.writeline("Good bye"); exit(0); } else IS.writeline("You aren't speaking the right protocol"); } } }