Example #1
0
File: ztcp.c Project: chungy/zsnes
int SendDataUDPNop()
{
   return (SendDataUDP(PacketSendSize,PacketSendArray));
}
Example #2
0
int main(int argc, char *argv[])
{
  int count;                   /* buffers to send */
  unsigned long int size;      /* bytes to send */
  int fd, TCPsock;
  struct sockaddr client_addr; /* the address of the client */ 
  struct sockaddr_in sad;      /* Address of the server */
  struct hostent *hp;
  struct protoent *ptrp;       /* pointer to a protocol table entry */ 
  char buf[MAXBUF];            /* buffer to hold datagram */
  int port;                    /* server port */
  int i;
  double s1,s2,total;
  char *slash;
  float rate;
  float initwindow;            /* max packets in flight */
  char action[4];              /* denotes the current send or recv */ 
  int retval;
  char *targets;
  int temp_addr;
  char *ports;
  int connectfd;

  fprintf(stderr,"\nSINGLE UDP DATA MOVER:\n%s %s %s %s %s %s <<%s>>\n\n",
	  argv[0],argv[1],argv[2],argv[3],argv[4],argv[5],argv[6]);
 

  if (argc != 7 && argc != 8){
    printf("Usage: %s <target_host> <port remotefile> <srcfile_descriptor> <srcfile_size> <IBP_srccap>\n", 
	   argv[0]);
    exit(3);
  }

  strcpy(glb.IBP_cap,argv[6]);
  targets=argv[1];
  ports=argv[2]; 





  /* Map TCP transport protocol name to protocol number */
  if (((int)(ptrp = getprotobyname("tcp"))) == 0) {
    fprintf(stderr, "cannot map \"tcp\" to protocol number");
    exit(1);
  }
  



  /* ----------- Create a TCP socket --------------- */
  TCPsock = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
  if (TCPsock < 0) {
    fprintf(stderr, "socket creation failed\n");
    exit(1);
  }


  /* Host and port come from input arguments and from server */
  /* Fill in the server's TCP address */
   hp = gethostbyname(argv[1]);
   if(hp == NULL) {
    fprintf(stderr, "%s: unknown host",argv[1]);
    printf("Error h_errno %d\n", h_errno);
    exit(2);
  }



  fprintf(stderr,"--->after gethost\n");
  bzero((char *)&sad, sizeof(sad));
  sad.sin_family = AF_INET;
  bcopy((char *)hp->h_addr, (char *)&sad.sin_addr, hp->h_length);
  sad.sin_port = htons((u_short)atoi(argv[2]));

  /* Connect the socket to the specified server */
  if (connect(TCPsock, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
    perror("connect failed");
    exit(1);
  }


 
   
  fprintf(stderr,"after connect \n");
  /* -------- create a  UDP  socket --------------- */
  UDPsock = socket(AF_INET, SOCK_DGRAM, 0);
  if(UDPsock < 0) {
    perror("Opening datagram socket");
    exit(1);
  }

  bzero((char *)&name1, sizeof(name1));
  name1.sin_family = AF_INET;
  name1.sin_addr.s_addr = sad.sin_addr.s_addr;
  name1.sin_port =/* 0*/htons((u_short)atoi(argv[2]));;

  length1 = sizeof(name1);

  /* ----------------- get the server port ------------------ */
  recv(TCPsock, &name1.sin_port, sizeof(name1.sin_port), 0);

  if (sendto(UDPsock, argv[3], strlen(argv[3])+1, 0, (struct sockaddr *)&name1, sizeof(name1)) < 0) 
       perror("Couldn't send filename");

  initwindow = (argc == 6) ?  atoi(argv[5]) : MINWIN;           /* decide initwindow */

  fd = atoi(argv[3]);
  size = atol(argv[4]);
  count = ((size-1) / MAXLOAD) + 1;                             /* compute # blocks of file */ 

  /* --- get file size needs to know how many blocks to transfer --- */
  if ((read(TCPsock, buf, MAXBUF*sizeof(char)) < 0))
      perror("Couldn't read file size");


  /**fprintf(stderr,"count-->%d  total size-->%ld    UDPsock-->%d  fd-->%d initwindow=%f\n",
	   count,size,UDPsock,fd,initwindow);   debug */

  s1 = seconds();
  retval = SendDataUDP(fd,UDPsock,size,count,initwindow);       /* Send Data through socket */
  s2 = seconds();

  total = s2-s1;
  rate = (total !=0) ? size/(1024.0*1024*(total)) : 0;
  fprintf(stderr,"\nsize = %d, time=%6.2f  xfer rate = %.2f MB/s (%.2fMb/s)\n",
	  size, total, rate, rate*8);
  close(fd);
  exit(0);
}