本文整理汇总了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;
}