当前位置: 首页>>代码示例>>C++>>正文


C++ NetAddress::GetAddress方法代码示例

本文整理汇总了C++中NetAddress::GetAddress方法的典型用法代码示例。如果您正苦于以下问题:C++ NetAddress::GetAddress方法的具体用法?C++ NetAddress::GetAddress怎么用?C++ NetAddress::GetAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NetAddress的用法示例。


在下文中一共展示了NetAddress::GetAddress方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: sscanf

// Init (client side)
status_t
InsecureConnection::Init(const char* parameters)
{
PRINT(("InsecureConnection::Init\n"));
	if (!parameters)
		return B_BAD_VALUE;
	status_t error = AbstractConnection::Init();
	if (error != B_OK)
		return error;
	// parse the parameters to get a server name and a port we shall connect to
	// parameter format is "<server>[:port] [ <up> [ <down> ] ]"
	char server[256];
	uint16 port = kDefaultInsecureConnectionPort;
	int upStreamChannels = kDefaultUpStreamChannels;
	int downStreamChannels = kDefaultDownStreamChannels;
	if (strchr(parameters, ':')) {
		int result = sscanf(parameters, "%255[^:]:%hu %d %d", server, &port,
			&upStreamChannels, &downStreamChannels);
		if (result < 2)
			return B_BAD_VALUE;
	} else {
		int result = sscanf(parameters, "%255[^:] %d %d", server,
			&upStreamChannels, &downStreamChannels);
		if (result < 1)
			return B_BAD_VALUE;
	}
	// resolve server address
	NetAddress netAddress;
	error = NetAddressResolver().GetHostAddress(server, &netAddress);
	if (error != B_OK)
		return error;
	in_addr serverAddr = netAddress.GetAddress().sin_addr;
	// open the initial channel
	Channel* channel;
	error = _OpenClientChannel(serverAddr, port, &channel);
	if (error != B_OK)
		return error;
	error = AddUpStreamChannel(channel);
	if (error != B_OK) {
		delete channel;
		return error;
	}
	// send the server a connect request
	ConnectRequest request;
	request.protocolVersion = B_HOST_TO_BENDIAN_INT32(kProtocolVersion);
	request.serverAddress = serverAddr.s_addr;
	request.upStreamChannels = B_HOST_TO_BENDIAN_INT32(upStreamChannels);
	request.downStreamChannels = B_HOST_TO_BENDIAN_INT32(downStreamChannels);
	error = channel->Send(&request, sizeof(ConnectRequest));
	if (error != B_OK)
		return error;
	// get the server reply
	ConnectReply reply;
	error = channel->Receive(&reply, sizeof(ConnectReply));
	if (error != B_OK)
		return error;
	error = B_BENDIAN_TO_HOST_INT32(reply.error);
	if (error != B_OK)
		return error;
	upStreamChannels = B_BENDIAN_TO_HOST_INT32(reply.upStreamChannels);
	downStreamChannels = B_BENDIAN_TO_HOST_INT32(reply.downStreamChannels);
	port = B_BENDIAN_TO_HOST_INT16(reply.port);
	// open the remaining channels
	int32 allChannels = upStreamChannels + downStreamChannels;
	for (int32 i = 1; i < allChannels; i++) {
PRINT(("  creating channel %ld\n", i));
		// open the channel
		error = _OpenClientChannel(serverAddr, port, &channel);
		if (error != B_OK)
			RETURN_ERROR(error);
		// add it
		if (i < upStreamChannels)
			error = AddUpStreamChannel(channel);
		else
			error = AddDownStreamChannel(channel);
		if (error != B_OK) {
			delete channel;
			return error;
		}
	}
	return B_OK;
}
开发者ID:SummerSnail2014,项目名称:haiku,代码行数:83,代码来源:InsecureConnection.cpp


注:本文中的NetAddress::GetAddress方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。