/* * main() - parse command line, create a socket, handle requests */ int main(int argc, char **argv) { /* for getopt */ long opt; int lru_size = 10; int port = 9000; check_team(argv[0]); /* parse the command-line options. They are 'p' for port number, */ /* and 'l' for lru cache size. 'h' is also supported. */ while ((opt = getopt(argc, argv, "hl:p:")) != -1) { switch(opt) { case 'h': help(argv[0]); break; case 'l': lru_size = atoi(argv[0]); break; case 'p': port = atoi(optarg); break; } } /* open a socket, and start handling requests */ int fd = open_server_socket(port); handle_requests(fd, file_server, lru_size); exit(0); }
void check_fds_states(t_serv *serv, int type) { char *cmd; t_client *tmp; tmp = (type == 0) ? serv->client : serv->gfx; while (tmp != NULL) { if (FD_ISSET(tmp->fd, &(serv->readfds))) { if ((cmd = tmp->fct_read(serv, tmp->fd)) == NULL) return; if (tmp->connected == 1) fill_cmd(cmd, tmp, serv); else check_team(serv, tmp, cmd); } if (tmp->cmd != NULL && tmp->need_write == 1) if (FD_ISSET(tmp->fd, &(serv->writefds))) { tmp->fct_write(serv, tmp, tmp->shortest_cmd); update_client(tmp, serv); } tmp = tmp->next; } }
/* * main() - parse command line, open a socket, transfer a file */ int main(int argc, char **argv) { /* for getopt */ long opt; char *server = NULL; char *put_name = NULL; char *get_name = NULL; int port; char *save_name = NULL; check_team(argv[0]); /* parse the command-line options. */ while ((opt = getopt(argc, argv, "hs:P:G:S:p:")) != -1) { switch(opt) { case 'h': help(argv[0]); break; case 's': server = optarg; break; case 'P': put_name = optarg; break; case 'G': get_name = optarg; break; case 'S': save_name = optarg; break; case 'p': port = atoi(optarg); break; } } /* open a connection to the server */ int fd = connect_to_server(server, port); /* put or get, as appropriate */ if (put_name) put_file(fd, put_name); else get_file(fd, get_name, save_name); /* close the socket */ int rc; if ((rc = close(fd)) < 0) die("Close error: ", strerror(errno)); exit(0); }