UString GameTime::getWeekString() const { return format("%s %d", tr("Week"), getWeek()); }
main (int argc, char *argv[]) { int i; /* loop index */ int n; /* number of chars read */ char *user, *pass, *svc; /* given by command line args */ char host[]="xinu00.cs.purdue.edu"; /* location of server */ int len; /* string length temporaries */ struct cmd *pcmd; /* ptr to a registration command*/ struct hostent *phe; /* pointer to host info. entry */ struct protoent *ppe; /* ptr. to protocol info. entry */ struct sockaddr_in socin; /* an IPv4 endpoint address */ int addrlen; /* len of a sockaddr_in struct */ int sock; /* descriptor for socket */ char buffer[BUFF_SIZ]; /* input buffer for prompt */ char reply[] = "This is an echo from the reg side -> "; /* reply prefix */ char replybuf[BUFF_SIZ+sizeof(reply)]; /* reply buffer */ /* check args */ int q = 0; if (q == 1) { memset(replybuf, 0, BUFF_SIZ+sizeof(reply)); if (getWeek(replybuf, BUFF_SIZ + sizeof(reply)) < 0) printf("Failure"); else printf("%s - ", replybuf); exit(1); } if (q == 2) { int rets; rets = saveWeek("W+class1|M|14:30|50+class1|W|14:30|50+class1|F|14:30|50+class2|T|10:00|75+class2|R|10:00|75+"); if (rets == 0) { printf("Win\n"); } else { printf("Fail\n"); } exit(1); } if (argc != 4) { fprintf(stderr, "use is: ./example_reg user passwd service\n"); exit(1); } user = argv[1]; pass = argv[2]; svc = argv[3]; if (strlen(user) > UID_SIZ) { fprintf(stderr, "user name %s is too long\n", user); exit(1); } if (strlen(pass) > PASS_SIZ) { fprintf(stderr, "password %s is too long\n", pass); exit(1); } if (strlen(svc) > SVC_SIZ) { fprintf(stderr, "Service name %s is too long\n", svc); exit(1); } while (1) { /* Open socket used to connect to inflection server */ memset(&socin, 0, sizeof(socin)); socin.sin_family = AF_INET; /* Map host name to IP address or map dotted decimal */ if ( phe = gethostbyname(host) ) { memcpy(&socin.sin_addr, phe->h_addr, phe->h_length); } else if ( (socin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE ) { fprintf(stderr, "can't get host entry for %s\n", host); exit(1); } socin.sin_port = htons( (unsigned short)TCPPORT ); ppe = getprotobyname("tcp"); /* Create the socket */ sock = socket(PF_INET, SOCK_STREAM, ppe->p_proto); if (sock < 0) { fprintf(stderr, "cannot create socket\n"); exit(1); } /* Connect the socket */ if (connect(sock, (struct sockaddr *)&socin, sizeof(socin)) < 0) { fprintf(stderr, "can't connect to port %d\n", TCPPORT); exit(1); } /* Form a registration command and send */ pcmd = (struct cmd *) buffer; pcmd->cmdtype = CMD_REGISTER; /* Add user ID */ len = strlen(user); memset(pcmd->cid, ' ', UID_SIZ); memcpy(pcmd->cid, user, len); pcmd->cslash1 = '/'; /* Add password */ len = strlen(pass); memset(pcmd->cpass, ' ', PASS_SIZ); memcpy(pcmd->cpass, pass, len); pcmd->cslash2 = '/'; /* Add service */ len = strlen(svc); memset(pcmd->csvc, ' ', SVC_SIZ); memcpy(pcmd->csvc, svc, len); pcmd->dollar = '$'; /* Send registration message */ send(sock, buffer, sizeof(struct cmd), 0); /* Wait for access app to respond by sending data */ int bytes_read = 0; int read_all = 1; while (read_all) { n = read(sock, &buffer[bytes_read], BUFF_SIZ); if (n < 0) { fprintf(stderr, "error reading from the socket\n"); exit(1); } else if (n == 0) { fprintf(stderr, "\nTCP connection was closed before a prompt arrived\n"); exit(0); } printf("Recieved: %.*s\n", n, buffer + bytes_read); int i; for (i = 0; i < n; i++) { if (buffer[bytes_read + i] == '\0') { read_all = 0; } } bytes_read += n; } /* prompt arrived from access app */ n = bytes_read; buffer[n] = '\0'; fprintf(stderr, "\nReceived a prompt: %s\n", buffer); memset(replybuf, 0, BUFF_SIZ+sizeof(reply)); int num = 0; if (strcmp(buffer, "MY_WEEK") == 0) { if (getWeek(replybuf, BUFF_SIZ + sizeof(reply)) < 0) printf("Failure\n"); else printf("Sucess\n"); } else { int ret; ret = saveWeek(buffer); replybuf[0] = ret; } len = strlen(replybuf); send(sock, replybuf, len, 0); replybuf[len+n] = '\0'; fprintf(stderr, "Sent a reply: %s\n", replybuf); fprintf(stderr, "\nClosing the TCP connection.\n"); close(sock); memset(buffer, 0, BUFF_SIZ); memset(replybuf, 0, BUFF_SIZ); } }