本文整理汇总了C++中SockAddr::Type方法的典型用法代码示例。如果您正苦于以下问题:C++ SockAddr::Type方法的具体用法?C++ SockAddr::Type怎么用?C++ SockAddr::Type使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SockAddr
的用法示例。
在下文中一共展示了SockAddr::Type方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SendData
/**
*
*
* @param message
* @param message_size
* @param port
* @param outPrefix [in] - If not null, then every response line will be
* prefixed with this string.
*
* @return bool
*/
static bool SendData(const char *message, size_t message_size, const SockAddr &connectAddr, const char *outPrefix = NULL)
{
size_t totalLength;
vector<char> buffer(max(MaxReplyLineSize, MaxCommandSize));
Socket sendSocket;
FileHandle fileHandle;
uint32_t magic;
if (!sendSocket.OpenTCP(connectAddr.Type()))
{
fprintf(stderr, "Error creating %s socket: %s\n",
Addr::TypeToString(connectAddr.Type()),
SystemErrorToString(sendSocket.GetLastError()));
return false;
}
if (!sendSocket.Connect(connectAddr))
{
fprintf(stderr, "Error connecting to beacon on %s: %s\n",
connectAddr.ToString(),
SystemErrorToString(sendSocket.GetLastError()));
return false;
}
// Assemble the message
magic = htonl(MagicMessageNumber);
memcpy(&buffer.front(), &magic, sizeof(uint32_t));
totalLength = message_size + sizeof(uint32_t);
if (totalLength > MaxCommandSize)
{
fputs("Command too long. Not Sent.\n", stderr);
return false;
}
memcpy(&buffer[sizeof(uint32_t)], message, message_size);
// Send our message.
if (!sendSocket.Send(&buffer.front(), totalLength))
{
fprintf(stderr, "Error sending command to beacon: %s\n", SystemErrorToString(sendSocket.GetLastError()));
return false;
}
// Use stdio for simpler reading of the socket.
fileHandle = fdopen(sendSocket, "r");
if (!fileHandle.IsValid())
{
perror("Error opening socket file: ");
return false;
}
// Read until done
while (fgets(&buffer.front(), buffer.size(), fileHandle))
{
if (outPrefix)
fputs(outPrefix, stdout);
fputs(&buffer.front(), stdout);
}
if (ferror(fileHandle))
{
perror("\nConnection failed. Partial completion may have occurred: \n");
return true;
}
return true;
}