协议不支持 C++ 地址系列
c++ address family not supported by protocol
我编写了一个方法,该方法创建一个套接字,将其连接到端点,然后返回其描述符:
static int open_socket(const char* host, unsigned short port)
{
#ifdef USE_IPV4
struct hostent* _hostent;
struct sockaddr_in _sockaddr_in;
// Variables
size_t sockaddr_len;
int sock_family;
int sock_type;
int sock_protocol;
int sockfd;
_hostent = gethostbyname(host);
if (_hostent == (struct hostent*) 0)
{
// Not Found
}
_sockaddr_in.sin_family = AF_INET;
sock_family = AF_INET;
sock_type = SOCK_STREAM;
sock_protocol = IPPROTO_TCP;
sockaddr_len = sizeof(_sockaddr_in);
(void*) memmove(&_sockaddr_in, _hostent->h_addr, _hostent->h_length);
_sockaddr_in.sin_port = htons(port);
// Now create socket
sockfd = socket(sock_family, sock_type, sock_protocol);
if (sockfd < 0)
{
// "Internal Error"
}
if (connect(sockfd, (struct sockaddr*) &_sockaddr_in, sockaddr_len) < 0)
{
std::cerr << strerror(errno) << std::endl;
std::cerr << "Endpoint is unavailable" << std::endl;
return 0;
// "Unavailable"
}
return sockfd;
#endif
}
当我尝试连接套接字时发生错误。 strerror(errno( 返回"协议不支持的地址系列"。我不知道为什么会发生这种情况,因为在其他示例中,AF_INET很好地处理了IPPROTO_TCP
您需要
将地址存储在sockaddr_in::sin_addr
中。 当您调用 memmove(&_sockaddr_in, ...)
时,您正在覆盖整个结构(从 sin_family
开始(。
相关文章:
- 将数组的地址分配给变量并删除
- 空基优化子对象的地址
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 如何在c++程序中找到函数的地址
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 被解释为低级别const的const对象的地址
- 将地址分配给本地指针后,公共对象的变量将消失
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 内联程序集printf将整数解释为地址
- 为什么指针不写入类的地址?
- 如何在C++中获取该对象的类声明中对象的地址?
- 通过按地址访问变量
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 函数名是c中该函数的第一条指令的地址吗
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- ReadProcessMemory() 不适用于像 0x2840C6C68D8 这样的长地址
- CUDA:统一内存和指针地址的更改
- 协议不支持 C++ 地址系列
- 从套接字获取地址系列.Linux