/*
 * client.c 
 * Client-outline code for Programming Assignment #1
 * CS640 (Fall 2006).
 */


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define SERVER_PORT 4095

int main(int argc, char *argv[])
{
    int sockfd;
    struct sockaddr_in server_addr; /* connector's address information */
    int seq_num;
    char *server_ip = NULL;
    char *data = NULL;
    int server_port = SERVER_PORT;
    int request_type;
    int timeout;
    
    char c;
    while ((c = getopt (argc, argv, "hs:p:r:d:t:n:")) != -1)
      switch (c)
	{
	

	case 'h':
	  /* display help menu */
	  return 0;
	  break;
	  
	case 's':
	  
	  /* server-ip */
	   server_ip = optarg;
	   break;

	case 'p':
	  
	  /* server port number */
	  server_port = atoi(optarg);
	  break;

  	case 'r':
	  
	  /* request type */
	  /* Validate that its a correct request type */
	  request_type = atoi(optarg);
	  
	  break;

	case 't':
	  
	  /* timeout period */
	  timeout = atoi(optarg);
	  break;
 

	case 'd':
	  
	  /* data */
	  
	  data = optarg;
	  break;
 
	case 'n':
	  
	  /* seq-no */
	  
	  seq_num = atoi(optarg);
	  break;
 
	case '?':
	  
	  // unknown option
	  
	  if (isprint (optopt))
	    fprintf (stderr,
		     "Unknown option `-%c'.\n",
		     optopt);
	  else
	    fprintf (stderr,
		     "Unknown option character `\\x%x'.\n",
		     optopt);
	  return 1;
	
	default:
	  abort ();
	  

	}

    /* Handle some errors, i.e. incorrect arguments */

    /* Fill in the socket call
       sockfd = socket(....);
       handle error conditions
     */
       

    /* Fill in the server_addr structure */

    /* Construct the message to send
       Remember you will need to use inet_aton to fill in
       the IP address, in the data part (for type 0 message)
     */	

    /* Send the message:
       sendto(sockfd,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))
       handle errors
     */

    /* If this was a terminate server message, then the
       client should close the socket and exit.
     */

    /* Set the timeout to receive a response */
    /* You can use different system calls here:
       e.g. select, signal.
       Read their manpages to understand them and
       implement the timeout, while you wait for
       response.
     */

    /* If you are ready to receive a message
       make the appropriate call to recvfrom
       recvfrom(sockfd,...)
       check errors
       If response is a redirect message, send the query to the new server mentioned in the 
       redirect message.
     */

    /* Provide necessary output */
    
    /* Close the socket */
    close(sockfd);

    return 0;
}

