在进行套接字编程时,查找域名然后连接的正确方法是什么?
我编写过网络应用程序,但主要是用 Python 编写的。
我正在编写一个 C++ 应用程序,但对于查找域名并连接到其 IP 地址的语法应该是什么,我有点模糊。
具体来说,我不确定如何形成对套接字的 gethostbyname() 调用的结果。
我的代码目前看起来像这样:
const hostent* host = GetHost(); //This works correctly
if (!host) {
DebugMessage("Bad host", "Could not resolve host");
return NULL;
}
DebugMessage("Got host", host->h_name);
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in addr;
addr.sin_family = AF_INET;
//This funky cast is what i'm concerned about
addr.sin_addr.s_addr = (*((in_addr*)(host->h_addr_list[0]))).s_addr;
addr.sin_port = htons(80);
DebugMessage("addr", (*((in_addr*)(host->h_addr_list[0]))).s_addr);
int c = connect(s, (SOCKADDR*)&addr, sizeof(addr));
int le = WSAGetLastError();
if (c == SOCKET_ERROR) {
//etc...
它确实有效,但在我看来,用于分配 s_addr 的时髦转换对于我实际正在做的事情来说似乎太复杂了,导致我相信这不是它“应该完成的方式” ”。我是否太习惯高级语言了,这就是它应该的方式,或者我是否在某处缺少实用函数?
感谢您的帮助。
I've programmed networked applications, but primarily in Python.
I'm writing a C++ application and I'm a little fuzzy on exactly what the syntax should be to look up a domain name and connect to it's IP address.
Specifically, I'm not sure how to go form the result of a gethostbyname() call to a socket.
My code currently looks like this:
const hostent* host = GetHost(); //This works correctly
if (!host) {
DebugMessage("Bad host", "Could not resolve host");
return NULL;
}
DebugMessage("Got host", host->h_name);
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in addr;
addr.sin_family = AF_INET;
//This funky cast is what i'm concerned about
addr.sin_addr.s_addr = (*((in_addr*)(host->h_addr_list[0]))).s_addr;
addr.sin_port = htons(80);
DebugMessage("addr", (*((in_addr*)(host->h_addr_list[0]))).s_addr);
int c = connect(s, (SOCKADDR*)&addr, sizeof(addr));
int le = WSAGetLastError();
if (c == SOCKET_ERROR) {
//etc...
It does work, but It seems to me that that funky cast for assigning the s_addr seems too complex for what I'm actually doing, leading me to beleive that this is not the way it "should be done". Am I just too used to high level languages, and this is the way it's supposed to be, or am I missing a utility function somewhere?
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从技术上讲,您应该使用
h_addrtype
而不是假设AF_INET
,然后根据该类型解释h_addr_list
。一旦您已经假设AF_INET
(在socket
和connect
中),您也可以假设h_addr_list[0]<正如您所做的那样, /code> 是一个
in_addr
。如果您有 AAAA 记录 (IPv6),您可以检查h_addrtype
,这样您就可以拒绝它而不是错误地传送它。Technically you should use
h_addrtype
rather than assumeAF_INET
and then interpreth_addr_list
based on that type. Once you are already assumingAF_INET
(both in thesocket
and inconnect
) you may as well assume thath_addr_list[0]
is anin_addr
as you are doing. You could checkh_addrtype
in case you got an AAAA record (IPv6) so you can reject it rather than mis-cast it.我相信解析域名的最新且推荐的方法是使用
getaddrinfo
(Windows, linux)。据我所知,没有异步查找的标准方法,因此生成专门用于进行 DNS 查找的线程是很常见的。
I believe the latest and recommended way to resolve domain name is to use
getaddrinfo
(windows, linux).As far as I know, there is no standard way of making a lookup asynchronously, therefore it's quite common to spawn a thread dedicated to doing DNS lookups.