bool car_timestamp_sender::init_socket() {
	sock = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
	if (sock == INVALID_SOCKET) {
		printf("could not create socket\n");
		return false;
	}

	sockaddr_in service;
	service.sin_family = AF_INET;
	service.sin_addr.s_addr = INADDR_ANY;
	service.sin_port = htons(PORT - 1);

	if (bind(sock, (SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR) {
		printf("could not bind socket\n");
		cleanup_socket();
		return false;
	}

	int tval = 1;
	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&tval, (int)sizeof(tval)) == SOCKET_ERROR) {
		printf("could not set broadcast option, error: %d\r\n", WSAGetLastError());
		cleanup_socket();
		return false;
	}

	return true;
}
car_timestamp_sender::~car_timestamp_sender() {
	cleanup_thread();
	cleanup_socket();
	cleanup_wsa();

	timeEndPeriod(1);
}
Example #3
0
/**
 * Output error message and exit the program.  This is just for
 * internal use.
 *
 * @param fmt [in] format string, like printf.
 * @param ... [in] variable length argument like printf.
 *
 */
void
j_internal_error(char *fmt, ...)
{
    va_list ap;

    va_start(ap,fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);

    /* clean up socket if already opened */
    cleanup_socket();

    exit(1);
}
car_timestamp_sender::car_timestamp_sender() {
	wsa_init = false;
	if (!init_wsa()) {
		throw exception("init wsa failed\n");
	}
	
	if (!init_socket()) {
		cleanup_wsa();
		throw exception("init socket failed\n");
	}

	if (!init_thread()) {
		cleanup_socket();
		cleanup_wsa();
		throw exception("init thread failed\n");
	}

	timeBeginPeriod(1);
}
Example #5
0
static void cleanup_socket_on_signal(int sig)
{
	cleanup_socket();
	sigchain_pop(sig);
	raise(sig);
}
Example #6
0
int main(int argc, char **argv) {
	int new_sd = 0;
#ifdef HANDLE_MULTI
	int pid = 0;
#endif
	struct sockaddr_un server_address;
	struct sockaddr_un client_address;
	socklen_t client_address_length;
	char buf[1];


	if (argc < 2) {
		printf("%s %s\n", SOCKDEBUG_NAME, IDO_VERSION);
		printf("%s\n", IDO_COPYRIGHT);
		printf("Last Modified: %s\n", IDO_DATE);
		printf("%s\n", IDO_LICENSE);
		printf("\n");
		printf("Usage: %s <socketname>\n", argv[0]);
		printf("\n");
		printf("Creates a UNIX domain socket with name <socketname>, waits for a\n");
		printf("client to connect and then prints all received data to stdout.\n");
		printf("Only one client connection is processed at any given time.\n");
		exit(1);
	}

	/* initialize signal handling */
	signal(SIGQUIT, sighandler);
	signal(SIGTERM, sighandler);
	signal(SIGINT, sighandler);

	socketname = strdup(argv[1]);
	if (socketname == NULL) {
		perror("Could not dup socket name");
		exit(1);
	}

	/* create a socket */
	if (!(sd = socket(AF_UNIX, SOCK_STREAM, 0))) {
		perror("Cannot create socket");
		exit(1);
	}

	/* bind the socket */
	strncpy(server_address.sun_path, socketname, sizeof(server_address.sun_path));
	server_address.sun_family = AF_UNIX;
	if ((bind(sd, (struct sockaddr *)&server_address, SUN_LEN(&server_address)))) {
		perror("Could not bind socket");
		exit(1);
	}

	/* listen for connections */
	if ((listen(sd, 1))) {
		perror("Cannot listen on socket");
		cleanup_socket(sd, socketname);
		exit(1);
	}

	client_address_length = (socklen_t)sizeof(client_address);

	/* accept connections... */
	while (1) {

		if ((new_sd = accept(sd, (struct sockaddr *)&client_address, (socklen_t *)&client_address_length)) < 0) {
			perror("Accept error");
			cleanup_socket(sd, socketname);
			exit(1);
		}

#ifdef HANDLE_MULTI
		/* fork... */
		pid = fork();

		switch (pid) {
		case -1:
			perror("Fork error");
			cleanup_socket(sd, socketname);
			exit(1);
			break;

		case 0:
			/* print all data from socket to the screen */
			while ((read(new_sd, buf, sizeof(buf)))) {
				printf("%c", buf[0]);
			}
			exit(0);
			break;

		default:
			close(new_sd);
			break;
		}

#else
		/* print all data from socket to the screen */
		while ((read(new_sd, buf, sizeof(buf)))) {
			printf("%c", buf[0]);
		}

		close(new_sd);
#endif

	}

	/* cleanup after ourselves */
	cleanup_socket(sd, socketname);

	return 0;
}
Example #7
0
int main(int argc, char *argv[])
{
	int ret;

	enum {
		cmd_attach = 1,
		cmd_attachall,
		cmd_detach,
		cmd_port,
		cmd_list,
		cmd_help,
		cmd_version
	} cmd = 0;

	usbip_use_stderr = 1;

#ifdef __linux__
	if (geteuid() != 0)
		notice("running non-root?");
#endif

	if (init_socket())
		return EXIT_FAILURE;

 	ret = usbip_names_init(USBIDS_FILE);
 	if (ret)
 		notice("failed to open %s", USBIDS_FILE);

	for (;;) {
		int c;
		int index = 0;

		c = getopt_long(argc, argv, "adplvhDSx", longopts, &index);

		if (c == -1)
			break;

		switch(c) {
			case 'a':
				if (!cmd)
					cmd = cmd_attach;
				else
					cmd = cmd_help;
				break;
			case 'd':
				if (!cmd)
					cmd = cmd_detach;
				else
					cmd = cmd_help;
				break;
			case 'p':
				if (!cmd)
					cmd = cmd_port;
				else cmd = cmd_help;
				break;
			case 'l':
				if (!cmd)
					cmd = cmd_list;
				else
					cmd = cmd_help;
				break;
			case 'v':
				if (!cmd)
					cmd = cmd_version;
				else
					cmd = cmd_help;
				break;
#ifdef __linux__
			case 'x':
				if(!cmd)
					cmd = cmd_attachall;
				else
					cmd = cmd_help;
				break;
#endif
			case 'h':
				cmd = cmd_help;
				break;
			case 'D':
				usbip_use_debug = 1;
				break;
			case 'S':
				usbip_use_syslog = 1;
				break;
			case '?':
				break;

			default:
				err("getopt");
		}
	}

	/* disable output buffering (needed to read the pipe when launched from another program) */
	setbuf(stderr, NULL);

	ret = 0;
	switch(cmd) {
		case cmd_attach:
			if (optind == argc - 2)
				ret = attach_device(argv[optind], argv[optind+1]);
			else
				show_help();
			break;
		case cmd_detach:
			while (optind < argc)
				ret = detach_port(argv[optind++]);
			break;
		case cmd_port:
			ret = show_port_status();
			break;
		case cmd_list:
			while (optind < argc)
				ret = show_exported_devices(argv[optind++]);
			break;
		case cmd_attachall:
			while(optind < argc)
				ret = attach_devices_all(argv[optind++]);
			break;
		case cmd_version:
			printf("%s\n", version);
			break;
		case cmd_help:
			show_help();
			break;
		default:
			show_help();
	}


	usbip_names_free();
	
	cleanup_socket();

	exit((ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
}
/* usage: exename paramfile host [port] */
int main(int argc, char *argv[])
{
  int portnum;			///< Port number
  int sd;
  ConfigurationHeader conf;	///< Configuration of vector to be sent

  FILE *fp;
  int framelen;			///< Number of frames in source
  int fshift;			///< Frame shift of source
  unsigned short vecdim;	///< Vector dimension


  /* application argument check */
  if (argc < 3) {
    fprintf(stderr, "usage: %s paramfile hostname [portnum]\n", argv[0]);
    return -1;
  }

  /* open HTK parameter file and read header */
  if ((fp = fopen(argv[1], "r")) == NULL) {
    fprintf(stderr, "Error: failed to open %s\n", argv[1]);
    close_socket(sd); cleanup_socket(); return -1;
  }
  if (fread(&framelen, sizeof(int), 1, fp) != 1) {
    fprintf(stderr, "Error: failed to read header in %s\n", argv[1]);
    close_socket(sd); cleanup_socket(); return -1;
  }
  swap_bytes((char *)&framelen, sizeof(int), 1);
  if (fread(&fshift, sizeof(int), 1, fp) != 1) {
    fprintf(stderr, "Error: failed to read header in %s\n", argv[1]);
    close_socket(sd); cleanup_socket(); return -1;
  }
  swap_bytes((char *)&fshift, sizeof(int), 1);
  if (fread(&vecdim, sizeof(unsigned short), 1, fp) != 1) {
    fprintf(stderr, "Error: failed to read header in %s\n", argv[1]);
    close_socket(sd); cleanup_socket(); return -1;
  }
  swap_bytes((char *)&vecdim, sizeof(unsigned short), 1);
  vecdim /= sizeof(float);
  fseek(fp, sizeof(short), SEEK_CUR);

  printf("%d frames, %d fshift, %d vecdim\n", framelen, fshift, vecdim);

  /* prepare configuration header */
  /* Warning: no parameter type/size check performed at server side */
  conf.veclen = vecdim;			 /* dimension */
  conf.fshift = (float)fshift / 10000.0; /* msec/frame */
#ifdef OUTPROBVECTOR
  conf.outprob_p = 1;			 /* outprob vector */
#else
  conf.outprob_p = 0;			 /* feature vector */
#ifdef SEND26TO25
  conf.veclen--;
#endif
#endif

  /* port number: use VECINNET_PORT (= Julius default) if not specified */
  portnum = (argc >= 4) ? atoi(argv[3]) : VECINNET_PORT;


  /* connect to server */
  if ((sd = make_connection(argv[2], portnum)) < 0) {
    return -1;
  }

  /* send configuration header */
  if (send_data(sd, &conf, sizeof(ConfigurationHeader)) == -1) {
    return -1;
  }
    
  /* send data, frame by frame*/
  {
    int i;
    float *buf;

    buf = (float *)malloc(sizeof(float) * vecdim);
    for (i = 0; i < framelen; i++) {
      if (fread(buf, sizeof(float), vecdim, fp) != vecdim) {
	fprintf(stderr, "Error: failed to read vectors in %s\n", argv[1]);
	close_socket(sd); cleanup_socket(); return -1;
      }
      swap_bytes((char *)buf, sizeof(float), vecdim);

#if !defined(OUTPROBVECTOR) && defined(SEND26TO25)
      /* send 26 dim data as 25 dim */
      {
	int j;
	for (j = 13; j < vecdim; j++) {
	  buf[j-1] = buf[j];
	}
	if (send_data(sd, buf, sizeof(float) * (vecdim - 1)) == -1) {
	  close_socket(sd); cleanup_socket(); return -1;
	}
      }
#else
      if (send_data(sd, buf, sizeof(float) * vecdim) == -1) {
	close_socket(sd); cleanup_socket(); return -1;
      }
#endif
      printf("frame %d\n", i);
    }
  }
  /* send end-of-utterance at each end of utterance unit */
  if (send_end_of_utterance(sd) == -1) {
    close_socket(sd); cleanup_socket(); return -1;
  }

  /* send end-of-session when input was ended */
  if (send_end_of_session(sd) == -1) {
    close_socket(sd); cleanup_socket(); return -1;
  }

  /* close socket */
  close_socket(sd);
  cleanup_socket();

  return 0;
}