// this is the client program to test libipq; it connects to a remote
// server, sends a few bytes, then closes the connection.  Compile with:
// gcc -g -Wall client.c -o client

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>

int connectSocket(int connect_port,char *address)
{
  struct sockaddr_in a;
  int s,retval;

  if(connect_port<0){
    fprintf(stderr,"Invalid port %d passed",connect_port);
    return -1;
  }

  if(address==NULL){
    fprintf(stderr,"NULL address passed");
    return -1;
  }

  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0) {
    fprintf(stderr,"Error creating socket: %s",strerror(errno));
    return -1;
  }

  memset(&a, 0, sizeof (struct sockaddr_in));
  a.sin_port = htons(connect_port);
  a.sin_family = AF_INET;

  if(!inet_aton(address, (struct in_addr *) &a.sin_addr.s_addr)){
    fprintf(stderr,"Bad IP address format %s: %s",address,strerror(errno));
    close(s);
    return -1;
  }

  retval=connect(s,(struct sockaddr *)&a,sizeof (struct sockaddr_in));

  return s;
}

int main()
{
  int sock;
  ssize_t bytes;
  char buf[1000];
  long long counter;

  counter=0;
  while(1){
    if((counter%1000)==0){
      fprintf(stderr,"Counter is %lld\n",counter);
    }
    sock=connectSocket(8888,"192.168.3.3");

    snprintf(buf,1000,"hello%lld",counter);
    bytes=send(sock,buf,strlen(buf),0);
    usleep(100);

    counter++;

    close(sock);
  }

  return 0;
}
