#include #include #include #include #include #include #include #include #include "p1.h" void usage(char *err_msg) { fprintf(stderr, "\n%s\nUsage: client [-h :] -u -f -l \n\n", err_msg); exit(1); } /*[]------------------------------------------------------------------[] [] client [-h :] -u -f -l []------------------------------------------------------------------[]*/ extern char *optarg; extern int opterr, optind, optopt; int parse_cmd_arg(int argc, char **argv, char *srv_hostname, int *srv_port, char *uid, char *firstname, char *lastname) { char opt_char; int got_host = 0; char *p; /* to turn off default report of illegal option, uncomment the next line */ /* opterr = 0; */ while ((opt_char = getopt(argc, argv, "h:u:f:l:")) != EOF) { switch (opt_char) { case 'h': if ( !(p = strchr(optarg, ':')) ) usage("No port number indicated"); *p = 0; /* break it down */ strcpy(srv_hostname, optarg); *srv_port = atoi(p+1); got_host = 1; break; case 'u': strcpy(uid, optarg); break; case 'f': strcpy(firstname, optarg); break; case 'l': strcpy(lastname, optarg); break; case '?': usage(""); break; } } if (optind != argc) usage("Too many arguments"); if (optind < 7) usage("Too few arguments"); if ((optind == 7) && (got_host)) usage("Too few arguments"); /* one argument is missing, but not "-h" */ return optind; /* optind = 7 (without -h) or 9(with -h) */ } void send_message(int sock, char *buf, int size){ /* Send char buffer on socket */ int err = -1; /* err = send (sock, ... Make sure you check the error conditions ... */ } void recv_message(int sock, char *buf, int size){ /* Receive char buffer on socket */ /* NOTE: * TCP only provides byte stream service. * So, even though the server sends a whole line (with \n at the end * you may get a partial line * - - - - - - - - - - - - - - - - * Your code should check to make sure the last character it received is * a newline --- else it should go back and read again * - - - - - - - - - - - - - - - - * At first, you should just ignore this condition, but to get full credit * you should turn in a version that actually does this */ int err = -1; /* err = recv (sock, ... Make sure you check the error conditions ... */ } int main (int argc, char *argv[]) { struct sockaddr_in sin; int sock; char client_msg[MAXSTRSIZE], srv_msg[MAXSTRSIZE]; int client_msg_len, srv_msg_len; int err; int srv_port; char srv_hostname[MAXSTRSIZE], uid[MAXSTRSIZE]; char firstname[MAXSTRSIZE], lastname[MAXSTRSIZE]; /* * parse command-line argument */ if (parse_cmd_arg(argc, argv, srv_hostname, &srv_port, uid, firstname, lastname) == 7) { printf("\n [client] Using default server:port at %s:%d\n\n", SERVER_HOSTNAME, SERVER_PORT); strcpy(srv_hostname, SERVER_HOSTNAME); srv_port = SERVER_PORT; } /* * Allocate a socket (socket type SOCK_STREM for TCP) * * --------------------------------- * sock = socket (...); * Check sock for error ( sock < 0) => error * --------------------------------- */ /* * fill in '' * * --------------------------------- * bzero((char *)&sin, sizeof(sin)); * sin.sin_family = ...; * sin.sin_addr.s_addr = ...; * sin.sin_port = ...; * --------------------------------- */ /* * Connect to the server. * * --------------------------------- * err = connect(sock, ...); * check error code! * --------------------------------- */ /* * Prepare * Send server the HELLO message, * * --------------------------------- * send_message (sock, ...); * --------------------------------- */ /* * packet being sent without errors, print proper message with the name of student */ /* * wait for server's ACK message, put it into * * --------------------------------- * recv_message (sock, ...); * --------------------------------- */ /* parse out the cookie using sscanf or something similar */ /* Prepare with BYE msg * Send BYE to server * * --------------------------------- * send_message (sock, ...); * --------------------------------- */ return 0; }