/***********************************************
 * released under (E) licensing ...            *
 *        (E) RULES AND REGULATIONS            *
 * permission to use/rewrite/add     : granted *
 * permission to trojan/steal        : denied  *
 * permission to use illegally       : denied  *
 * permission to use on /dev/urandom : denied  *
 ***********************************************/
/* contact el8@press.co.jp for full license    */
/* code copyrighted by ~el8 -- don't infringe! */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <netinet/in.h>

#define ERRSTR "\033[1;33m!\033[m"
#define SYNSTR "\033[1;32m*\033[m"

char *msg[]={"how now brown cow?",
            "yo, what is up?   ",
            "yo, gihjrwo       ",
            "FINISHSHHSHGSDG   "};
void sig(int s);
int udpsock,tcpsock;
void 
main(int argc,char *argv[])
{
  //int udpsock,tcpsock;
  struct sockaddr_in tcps,udps;
  char buf[255];
  int n=0,tcpbytes=0,udpbytes=0,utotal=0,ttotal=0;
  int good=0;
  int x;
  if (!(argc==3)) {
    fprintf(stderr,"usage: %s <ip> <tcp port>\n",argv[0]);
    exit(-1);
  }
  signal(SIGTERM,sig);
  signal(SIGINT,sig);
  signal(SIGKILL,sig);
  signal(SIGSEGV,sig);
 
  
  if ((udpsock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
            perror("socket(SOCK_DGRAM)");
            exit(-1);
  }
  if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror("socket(SOCK_STREAM)");
            exit(-1);
  }
  signal(SIGINT,sig);
  udps.sin_family = AF_INET;
  udps.sin_addr.s_addr = inet_addr(argv[1]);
  
  tcps.sin_family = AF_INET;
  tcps.sin_addr.s_addr = inet_addr(argv[1]);
  tcps.sin_port = htons(atoi(argv[2]));
  
  if (connect(tcpsock,(struct sockaddr *)&tcps,sizeof(tcps))< 0) {
    perror("couldn't start a conversation with server!");
    exit(-1);
  }

  if (send(tcpsock,"START",5,0)==-1)
  {
     perror("couldn't send starter msg");
     exit(-1);
  }
  if (recv(tcpsock,buf,sizeof(buf),0)==-1)
  {
     perror("server wouldnt respond to us :( ");
     exit(-1);
  }
  if (sscanf(buf,"UDP %i",&x)!=1)
  {
     fprintf(stderr,"incorrect server response, check TCP port, try again.\n");
     exit(-1);
  }
  udps.sin_port = htons(x);
  
  fprintf(stderr,"%s [UDP PORT #%i] [TCP PORT #%i] [sending]\n",argv[1],x,
	                                                        atoi(argv[2]));
  for(n=0;msg[n]!=NULL;n++){
     
     udpbytes=sendto(udpsock, msg[n], strlen(msg[n]), 0,
		    (struct sockaddr*)&udps, sizeof(struct sockaddr));
     tcpbytes = send(tcpsock, msg[n], strlen(msg[n]), 0);
     
     if (tcpbytes!=udpbytes) fputs(ERRSTR,stderr); 
                             else { fputs(SYNSTR,stderr); good++; }
     if (tcpbytes>0) ttotal+=tcpbytes;
     if (udpbytes>0) utotal+=udpbytes;
  }
  
  if (send(tcpsock,"DONE",5,0)==-1) {
     perror("couldnt tell the server we finished!"); exit(-1); }
  fprintf(stderr,"----------------------------[%s]--\n",argv[1]);
  fprintf(stderr,"UDP:\t[%i bytes sent]\n",utotal);
  fprintf(stderr,"TCP:\t[%i bytes sent]\n",ttotal);
  fprintf(stderr,"(%i synchronized packets sent [%i bad])\n",good,(n-good));
             
  close(udpsock);
  close(tcpsock);
  exit(0);
}
void sig(int s)
{
   send(tcpsock,"DONE",5,0);
   printf("\n%i\n",s);
   sleep(1);
   close(tcpsock);
   close(udpsock);
   exit(1);
}


syntax highlighted by Code2HTML, v. 0.9.1