meridiandemo.h

来自「件主要用于帮助计算机爱好者学习蚁群算法时做有关蚁群算法的试验。蚁群算法作为一种优」· C头文件 代码 · 共 56 行

H
56
字号
#ifndef MERIDIAN_DEMO_COMMON#define MERIDIAN_DEMO_COMMON//	Returns a query id based on the address and port//	of the current nodeuint64_t getNewQueryID(int sockFD) {	char hostname[HOST_NAME_MAX];	gethostname(hostname, HOST_NAME_MAX);	hostname[HOST_NAME_MAX - 1] = '\0';	struct hostent* he = gethostbyname(hostname);	if (he == NULL) {		perror("Cannot resolve localhost\n");		return Packet::to64(rand(), rand());	}	u_int localAddr = ntohl(((struct in_addr *)(he->h_addr))->s_addr);	u_short randVal = rand() % USHRT_MAX;	struct sockaddr_in addr;	socklen_t sockSize = sizeof(struct sockaddr_in);	if (getsockname(sockFD, (struct sockaddr *)&addr, 		&sockSize) == -1) {		perror("Getsockname error\n");		return Packet::to64(rand(), rand());	}	//	Concat with port	u_int secondParam = ntohs(addr.sin_port);	secondParam = secondParam << 16;	secondParam |= randVal;	//	Concat with address			return Packet::to64(localAddr, secondParam);}//	For parsing command line inputint parseHostAndPort(const char* nodeStr, NodeIdent& remoteNode) {	char hostname[HOST_NAME_MAX];	//	Hostname and port separated by ':'	const char* tmpStrPtr = strchr(nodeStr, ':');	if (tmpStrPtr == NULL) {		fprintf(stderr, "Invalid parameter, should be hostname:port\n");		return -1;	}	memcpy(hostname, nodeStr, tmpStrPtr - nodeStr);	hostname[tmpStrPtr - nodeStr] = '\0';	remoteNode.port = (u_short) atoi(tmpStrPtr + 1);	//	Get the add of remote host	struct hostent * tmp = gethostbyname(hostname);	if (tmp == NULL || tmp->h_addr_list == NULL) {		fprintf(stderr, "Can not resolve hostname of meridan node\n");		return -1;	}	remoteNode.addr = 		ntohl(((struct in_addr *)(tmp->h_addr))->s_addr);	return 0;}#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?