示例#1
0
bool CGetter::start()
{
    do
    {
        if (!get_ip_list())
            break;

        if (!send_http_request())
            break;

        if (!wait_response())
            break;

        return true;
    } while(false);

    return false;
}
示例#2
0
int main(int argc, char *argv[], char *dummy[])
{
	ushort tcp_port = 4711;
	struct srv_info sinfo;
	uint cmd;
	uint max_msglen;
	struct sockaddr_in srv_addr;
	int lstn_sd, peer_sd;
	int srv_id = 0, srv_cnt = 0;;

	own_node_addr = own_node();

	memset(&sinfo, 0, sizeof(sinfo));
		
	if (signal(SIGALRM, sig_alarm) == SIG_ERR)
		die("Server master: can't catch alarm signals\n");

	printf("******   TIPC Benchmark Server Started   ******\n");

	/* Create socket for communication with master: */
reset:
	master_sd = socket(AF_TIPC, SOCK_RDM, 0);
	if (master_sd < 0)
		die("Server: Can't create socket to master\n");

	if (bind(master_sd, (struct sockaddr *)&srv_ctrl_addr,
		 sizeof(srv_ctrl_addr)))
		die("Server: Failed to bind to master socket\n");

	/* Wait for command from master: */
	srv_from_master(&cmd, &max_msglen, 0, 0);
	buf = malloc(max_msglen);
	if (!buf)
		die("Failed to create buffer of size %u\n", ntohl(max_msglen));

	/* Create TIPC or TCP listening socket: */

	if (cmd == TIPC_CONN) {
		lstn_sd = socket (AF_TIPC, SOCK_STREAM,0);
		if (lstn_sd < 0)
			die("Server master: can't create listening socket\n");
		
		if (bind(lstn_sd, (struct sockaddr *)&srv_lstn_addr,
			 sizeof(srv_lstn_addr)) < 0)
			die("TIPC Server master: failed to bind port name\n");

		printf("******   TIPC Listener Socket Created    ******\n");
		srv_to_master(SRV_INFO, 0);
		close(master_sd);

	} else if (cmd == TCP_CONN) {
		if ((lstn_sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
			die("TCP Server: failed to create listener socket");

		/* Construct listener address structure */
		memset(&srv_addr, 0, sizeof(srv_addr));
		srv_addr.sin_family = AF_INET;
		srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
		srv_addr.sin_port = htons(tcp_port);
	
		/* Bind socket to address */
		while (0 > bind(lstn_sd, (struct sockaddr *) &srv_addr,
				sizeof(srv_addr)))
			srv_addr.sin_port = htons(++tcp_port);

		/* Inform master about own IP addresses and listener port number */
		get_ip_list(&sinfo, NULL);
		sinfo.tcp_port = htons(tcp_port);
		printf("******    TCP Listener Socket Created    ******\n");
		srv_to_master(SRV_INFO, &sinfo);
		close(master_sd);
	} else {
		close(master_sd);
		goto reset;
	}

	/* Listen for incoming connections */
	if (listen(lstn_sd, 32) < 0)
		die("Server: listen() failed");

	while (1) {
		if (waitpid(-1, NULL, WNOHANG) > 0) {
			if (--srv_cnt)
				continue;
			close(lstn_sd);
			printf("******      Listener Socket Deleted      ******\n");
			goto reset;
		}

		peer_sd = wait_for_connection(lstn_sd);
		if (!peer_sd)
			continue;
		srv_id++;
		srv_cnt++;
		if (fork()) {
			close(peer_sd);
			continue;
		}

		/* Continue in child process */
		close(lstn_sd);
		dprintf("calling echo: peer_sd: %u, srv_cnt = %u\n",peer_sd, srv_cnt);
		master_sd = socket(AF_TIPC, SOCK_RDM, 0);
		if (master_sd < 0)
			die("Server: Can't create socket to master\n");
		
		if (bind(master_sd, (struct sockaddr *)&srv_ctrl_addr,
			 sizeof(srv_ctrl_addr)))
			die("Server: Failed to bind to master socket\n");
		
		echo_messages(peer_sd, master_sd, srv_id);
	}
	close(lstn_sd);
	printf("******   TIPC Benchmark Server Finished   ******\n");
	exit(0);
	return 0;
}