#include<stdio.h>
#include <stdlib.h>
#include <netdb.h>
/*
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

*/

#define PORT            5555
#define MESSAGE         "Hola como estas"
#define SERVERHOST      "localhost"

void init_sockaddr (struct sockaddr_in *name,const char *hostname,uint16_t port);
void write_to_server (int);


int main(void)
{
      
        int sock;
        struct sockaddr_in servername;


        /* Crea el socket. */
        sock = socket (PF_INET, SOCK_STREAM, 0);
        if (sock < 0)
          {
            perror ("socket (client)");
            exit (EXIT_FAILURE);
          }


        /* conneccion al servidor. */
        init_sockaddr (&servername, SERVERHOST, PORT);
        if (0 > connect (sock, (struct sockaddr *) &servername, sizeof (servername)))
          {
            perror ("connect (client)");
            exit (EXIT_FAILURE);
          }


        /* Enviando data al servidor. */
        write_to_server (sock);
        close (sock);
        exit (EXIT_SUCCESS);
}


void init_sockaddr (struct sockaddr_in *name, const char *hostname, uint16_t port)
{
        struct hostent *hostinfo;
        name->sin_family = AF_INET;
        name->sin_port = htons (port);
        hostinfo = gethostbyname (hostname);
        if (hostinfo == NULL)
          {
            fprintf (stderr, "Unknown host %s.\n", hostname);
            exit (EXIT_FAILURE);
          }
        name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
}


void write_to_server (int filedes)
{
   int nbytes;
   nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
   if (nbytes < 0)
   {
      perror ("write");
      exit (EXIT_FAILURE);
   }
}
