/*  
    connect.c
    This example is set up to be run along with bind.c.  Execute bind.c in one 
    terminal, then run connect.c.  If running on same system, then
    no host name needs to be spedified.

    Usage :
      connect.c [HOST_NAME [PORT_NUMBER]]
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#define PROTOPORT 5139    /* default protocol port number */
char localhost[]= "localhost"; /* default host name */

/* Usage will be display if there is any incorrect option */
char usage[]= "\nThis is a client program which is set up to be run along with bind.c.\n\nUsage :\n      connect.c [HOST_NAME [PORT_NUMBER]]\n\n      HOST_NAME   : hostname of the machine on which bind.c is running.\n      PORT_NUMBER : TCP port number used by server.ch\n\nFor example :\n      connect.c\n      connect.c hostname\n      connect.c hostname 6000\n\nThe default optional values :\n      HOST_NAME   : localhost\n      PORT_NUMBER : 5139\n\n"; 

int main(int argc,char *argv[]) {
    struct hostent *ptrh;      /* pointer to a host table entry     */
    struct protoent *ptrp;     /* pointer to a protocol table entry */
    struct sockaddr_in sad;    /* structure to hold an IP address   */
    int    sd;                 /* socket descriptor                 */
    int port;                  /* protocol port number              */
    char *host;                /* pointer to host name              */
    int n;                     /* number of characters read         */
    char buf[1000];            /* buffer for data from the server   */
  
    setbuf(stdout,NULL);       /* turn off the out buffer           */
    memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
    sad.sin_family=AF_INET;             /* set family to internet   */
 
    port=PROTOPORT;          /* use default port number           */
    host=localhost;

    if(argc>2) {                 /* if protocol port specified        */
        port=atoi(argv[2]);      /* convert to binary                 */
    }
    else {
        port=PROTOPORT;          /* use default port number           */
    }
    if(port>0)                 /* test for legal value              */
        sad.sin_port=htons((u_short)port);
    else {                      /* print error message and exit      */
        fprintf(stderr,"\nError : bad port number %s\n",argv[1]);
        printf("%s", usage);
        exit(1);
    }

    /* Check host argument and assign host name.  */
    if(argc>1) {
        host=argv[1];            /* if host argument specified        */
    }
    else {
        host=localhost;
    }

    /* Convert host name to equivalent IP address and copy to sad.  */
    ptrh=gethostbyname(host);
    if(((char *)ptrh)==NULL) {
        fprintf(stderr,"\nError : invalid host: %s \n",host);
        printf("%s", usage);
        exit(1);
    }
    memcpy(&sad.sin_addr,ptrh->h_addr,ptrh->h_length);
  
    /* Map TCP transport protocol name to protocol number. */
    if(((int)(ptrp=getprotobyname("tcp")))==0) {
        fprintf(stderr,"\nError : cannot map \"tcp\" to protocol number");
        printf("%s", usage);
        exit(1);
    }
  
    /* Creat a socket */
    sd=socket(PF_INET,SOCK_STREAM,ptrp->p_proto);
    if(sd<0) {
        fprintf(stderr,"\nError : socket creation failed \n");
        printf("%s", usage);
        exit(1);
    }
  
    /* Connect the socket to the specified  server. */
    if(connect(sd,(struct sockaddr *)&sad,sizeof(sad))!=0) {
       fprintf(stderr,"\nError : connect failed\n");
       printf("%s", usage);
       close(sd);
       exit(1);
    }

    /* send a tcp package tp server */
    sprintf(buf,"From the Client\n");
    send(sd,buf,strlen(buf),0);

    /* receive a tcp package from server */
    recv(sd,buf,sizeof(buf),0);
    printf("\nData received: %s\n", buf); 

    close(sd);
    exit(0);
} 
