在进行套接字编程时,查找域名然后连接的正确方法是什么?

发布于 2024-12-02 12:07:18 字数 1015 浏览 0 评论 0原文

我编写过网络应用程序,但主要是用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开柳相依 2024-12-09 12:07:18

从技术上讲,您应该使用 h_addrtype 而不是假设 AF_INET,然后根据该类型解释 h_addr_list。一旦您已经假设AF_INET(在socketconnect中),您也可以假设h_addr_list[0]<正如您所做的那样, /code> 是一个 in_addr 。如果您有 AAAA 记录 (IPv6),您可以检查 h_addrtype,这样您就可以拒绝它而不是错误地传送它。

Technically you should use h_addrtype rather than assume AF_INET and then interpret h_addr_list based on that type. Once you are already assuming AF_INET (both in the socket and in connect) you may as well assume that h_addr_list[0] is an in_addr as you are doing. You could check h_addrtype in case you got an AAAA record (IPv6) so you can reject it rather than mis-cast it.

回忆那么伤 2024-12-09 12:07:18

我相信解析域名的最新且推荐的方法是使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文