#include "chat.h" #include "commands.c" int main (int argc, char *argv[]) { int port; if (argc < 2) { fprintf(stderr, "usage: server [port]\nusing default 41000.\n"); port = 41000; //exit(1); } else { port = atoi(argv[1]); } printf("Starting server.\n"); /* parse files and such */ FILE *pass = fopen("passwd","r"); passwd_t *passwd = passwd_new(pass); users_t *users = users_new(passwd); /* set up socket for listening */ int listen_sock, newcon, st_size; struct sockaddr_in here, there; listen_sock = socket(PF_INET, SOCK_STREAM, 0); fcntl(listen_sock, F_SETFL, O_NONBLOCK); here.sin_family = AF_INET; here.sin_port = htons(port); here.sin_addr.s_addr = INADDR_ANY; memset(here.sin_zero, '\0', (sizeof here.sin_zero)); if (bind(listen_sock, (struct sockaddr *)&here, (sizeof here))) { perror("bind(27)"); exit(3); } listen(listen_sock, 10); /* run until the server is quit. */ while (users_run(users)) { /* accept any waiting connections. */ st_size = sizeof there; newcon = accept(listen_sock,(struct sockaddr*)&there,&st_size); if (newcon != -1) /* we got a connection */ users_connect(users, newcon, (struct sockaddr*)&there); /* do any network activity that can be done */ users_do_network(users); } printf("Server quitting.\n"); }