示例#1
0
/*------------------------------------------------------------------------
 * clientUDPsock - allocate & connect a socket using UDP

   char	*destination;	/* Domain name or IP of the destination to connect to	
   int	portN;		/* destination port number	

   return:
      >0: socket allocated
      <0: error
	-2: invalid destination address
	-3: can't allocate socket
	-4: can't connect to destination

 *------------------------------------------------------------------------
**/
inline int clientUDPsock(const char *destination, int portN) 
{
  return clientsock(SOCK_DGRAM, destination, portN);
}
int main(int argc, char **argv)
{
	pthread_t thr;
	char	*host;
	int	port;
	int	i;
	int	c;
	char random_state[16];

	while ((c = getopt(argc, argv, "c:g:i:k:r:t:")) != EOF) {
		switch (c) {
		case 'c':
			num_connections = intarg(argv[0], optarg);
			break;
		case 'g':
			probability_get = intarg(argv[0], optarg);
			break;
		case 'i':
			num_iterations = intarg(argv[0], optarg);
			break;
		case 'k':
			num_keys = intarg(argv[0], optarg);
			break;
		case 'r':
			num_requests = intarg(argv[0], optarg);
			break;
		case 's':
			seed = intarg(argv[0], optarg);
			break;
		case 't':
			poll_wait_sec = intarg(argv[0], optarg);
			break;
		default:
			usage(argv[0]);
			break;
		}
	}

	initstate(seed, random_state, sizeof(random_state));

	if (argc < optind + 2)
		usage(argv[0]);

	host = argv[optind++];
	port = intarg(argv[0], argv[optind]);

	fds = malloc(num_connections * sizeof(int));
	if (fds == NULL) {
		perror("malloc");
		exit(1);
	}

	for (i = 0; i < num_connections; i++) {
		fds[i] = clientsock(host, port);
		if (fds[i] < 0) {
			perror(host);
			exit(1);
		}
		if ((i % 25) == 0) {
			printf("\rOpened %d connections", i);
			fflush(stdout);
		}

		set_nodelay(fds[i], 1);
		set_nonblock(fds[i]);
	}
	printf("\rOpened %d connections\n", num_connections);

	fd_in_use = calloc(1, fds[num_connections - 1]);
	if (fd_in_use == NULL) {
		perror("calloc");
		exit(1);
	}

	pthread_mutex_init(&stats_mutex, NULL);
	pthread_mutex_init(&fd_mutex, NULL);

	for (i = 0; i < num_requests; i++)
		pthread_create(&thr, NULL, thread_main, NULL);

	while (iteration_count < num_iterations) {
		poll(NULL, 0, 1000);
		stats();
	}

	stats();
	putchar('\n');
}
示例#3
0
inline int clientTCPsock(const char *destination, int portN) 
{
  return clientsock(SOCK_STREAM, destination, portN);
}