/*
 *	File	  : client.c
 *
 *	Title	  : Demo Client/Server.
 *
 *	Short	  : Example of a client using TCP protocol.	
 *
 *	Long 	  :
 *  
 *  Reference : W. Richard Stevens. "UNIX Network Programming".
 *              Prentice Hall Software Series. 1990.
 *              Chapter 6: Berkeley Sockets.
 *
 *	Author	  : Claudine Santos Badue.
 *
 *	Date	  : 28 August 2002.
 *
 *	Revised	  :
 */

#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>

#define REQ_SIZE       10
#define RES_SIZE       100
#define SERV_HOST_ADDR "150.164.10.1" /* host addr for server */
#define SERV_TCP_PORT  2035

int main(int argc, char *argv[]){
    struct sockaddr_in serv_addr;
    int sockfd;    
    char request[REQ_SIZE];
    char response[RES_SIZE];

    /*
     * Fill in the structure "serv_addr" with the address of the 
     * server that we want to connect with.
     */
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR);
    serv_addr.sin_port = htons(SERV_TCP_PORT);
    /*
     * Open a TCP socket (an Internet stream socket).
     */
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        fprintf(stderr,"client: can't open stream socket\n");
    /* 
     * Connect to the server.
     */
    if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr_in)) < 0)
      fprintf(stderr,"client: can't connect to server\n");    
    /* 
     * Set the request.
     */
    set_request(request); 
    /* 
     * Send the request to the server. 
     */
    if (send(sockfd, (void *)request, REQ_SIZE, 0) != REQ_SIZE){
      fprintf(stderr,"client: can't send request\n");    
      close(sockfd);
      exit(1);
    }    
    /* 
     * Receive the response from the server.
     */
    if (recv(sockfd, (void *)response, RES_SIZE, 0) != RES_SIZE){
      fprintf(stderr,"client: can't receive response\n");    
      close(sockfd);
      exit(1);
    }
    close(sockfd);
    exit(0);
}
