int main(int argc, char **argv){ if(argc < 4){ printf("Syntax Error\n"); printf("usage:\n\tmobilereminderd filename port secret\n"); exit(-1); } // initialize globals number_of_reminders = 0; next_reminder = 0; struct sigaction sigact; sigact.sa_handler = &reminder_handler; sigact.sa_flags = SA_RESTART; sigfillset(&sigact.sa_mask); if(sigaction(SIGALRM, &sigact, NULL) == -1){ printf("Error setting signal handler\n"); exit(-1); } global_port = DEFAULT_PORT; sscanf(argv[2], "%d", &global_port); secret_len = (int) strlen(argv[3]); strncpy(secret, argv[3], PAYLOAD_SIZE); read_reminders(argv[1]); alarm(alarm_times[0]); //listen for client registration mylisten(global_port, &myloop_condition, &mypacket_handler); }
int main(int argc, char **argv){ if(argc < 2){ printf("Syntax Error\n"); printf("usage:\n\tmpying ip-address port\n"); exit(-1); } //set globals loop = 1; global_port = DEFAULT_PORT; sscanf(argv[2], "%d", &global_port); gettimeofday(&before, NULL); char* buf = "oym8"; send_buffer(argv[1], global_port, buf, 4, EMPTY_FLAG); mylisten(global_port, &myloop_condition, &mypacket_handler); }
int main(int argc, char **argv){ if(argc < 2){ printf("Syntax Error\n"); printf("usage:\n\tmypingd port\n"); exit(-1); } struct sigaction sigact; sigact.sa_handler = &zombie_handler; sigact.sa_flags = SA_RESTART | SA_NOCLDSTOP; sigfillset(&sigact.sa_mask); if(sigaction(SIGCHLD, &sigact, NULL) == -1){ printf("Error setting signal handler\n"); exit(-1); } global_port = DEFAULT_PORT; sscanf(argv[1], "%d", &global_port); mylisten(global_port, &myloop_condition, &mypacket_handler); }
int main(int argc, char *argv[]) { struct sockaddr_in sin; mysocket_t bindsd; int len, opt, errflg = 0; char localname[256]; /* Parse the command line */ while ((opt = getopt(argc, argv, "")) != EOF) { switch (opt) { case '?': ++errflg; break; } } if (errflg || optind != argc) { fprintf(stderr, usage, argv[0]); exit(EXIT_FAILURE); } /* open connection on any available port */ if ((bindsd = mysocket()) < 0) { perror("mysocket"); exit(EXIT_FAILURE); } memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(0); len = sizeof(struct sockaddr_in); if (mybind(bindsd, (struct sockaddr *) &sin, len) < 0) { perror("mybind"); exit(EXIT_FAILURE); } if (mylisten(bindsd, 5) < 0) { perror("mylisten"); exit(EXIT_FAILURE); } if (local_name(bindsd, localname) < 0) { perror("local_name"); exit(EXIT_FAILURE); } fprintf(stderr, "Server's address is %s\n", localname); fflush(stderr); for (;;) { mysocket_t sd; /* just keep accepting connections forever */ if ((sd = myaccept(bindsd, (struct sockaddr *) &sin, &len)) < 0) { perror("myaccept"); exit(EXIT_FAILURE); } assert(sin.sin_family == AF_INET); fprintf(stderr, "connected to %s at port %u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); do_connection(sd); } /* end for(;;) */ if (myclose(bindsd) < 0) perror("myclose (bindsd)"); return 0; }