Ejemplo n.º 1
0
static void benchmark(char *title, char *cmd, int len) {
    client c;

    config.title = title;
    config.requests_issued = 0;
    config.requests_finished = 0;

    c = createClient(cmd,len,NULL);
    createMissingClients(c);

    config.start = mstime();
    aeMain(config.el);
    config.totlatency = mstime()-config.start;

    showLatencyReport();
    freeAllClients();
}
Ejemplo n.º 2
0
int main(int argc, char *args[]) {
	int port;
	int res;
	struct addrinfo *result = NULL, hints;
	SOCKET serverSocket = INVALID_SOCKET;
	char portbuf[16];

	struct timeval time;
	fd_set set;

	port = 5000;
	if (argc >= 2) {
		port = atoi(args[1]);
	}
	DBG_PRINT("server @ port %i", port);

	/* Initialize Winsock */
	res = WSAStartup(MAKEWORD(2,2), &wsaData);
	if (res != 0) {
	    DBG_PRINT("WSAStartup failed: %i", res);
	    return 1;
	}
	
	/* Setup server socket */
	ZeroMemory(&hints, sizeof (hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_flags = AI_PASSIVE;

	/* Resolve the local address and port to be used by the server */
	res = getaddrinfo(NULL, itoa(port, portbuf, 10), &hints, &result);
	if (res != 0) {
		DBG_PRINT("getaddrinfo failed: %i", res);
		goto error_server_setup0;
	}

	/* Create a SOCKET for the server to listen for client connections */
	serverSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
	if (serverSocket == INVALID_SOCKET) {
		printf("error at socket(): %ld", WSAGetLastError());
		goto error_server_setup1;
	}

	/* Setup the TCP listening socket */
    res = bind(serverSocket, result->ai_addr, (int)result->ai_addrlen);
    if (res == SOCKET_ERROR) {
		DBG_PRINT("bind failed with error: %ld", WSAGetLastError());
		goto error_server_setup2;
    }

	if ( listen( serverSocket, SOMAXCONN ) == SOCKET_ERROR ) {
		DBG_PRINT("listen failed with error: %ld", WSAGetLastError() );
		goto error_server_setup2;
	}

	/* Start accepting clients */
	do {
		FD_ZERO(&set);
		FD_SET(serverSocket, &set);

		time.tv_sec = 1;
		time.tv_usec = 0;
		if (select(0, &set, NULL, NULL, &time) > 0) {
			SOCKET clientSocket = INVALID_SOCKET;
			clientSocket = accept(serverSocket, NULL, NULL);
			if (clientSocket == INVALID_SOCKET) {
				DBG_PRINT("accept failed: %d", WSAGetLastError());
			} else {
				createControlClient(0, clientSocket);
			}
		}
	} while (g_serverRunning);

error_server_setup2:
    closesocket(serverSocket);
error_server_setup1:
    freeaddrinfo(result);
error_server_setup0:
	WSACleanup();

	/* Tidy */
	killAllClients();
	awaitAllClients();
	freeAllClients();

	DBG_PRINT("EXIT");

	return 0;
}