C 套接字错误 : "Name or service not known"

标签 c sockets getaddrinfo

我正在尝试用 C 语言编写一个程序,该程序使用套接字来获取网页。我的代码目前可以在某些网页上成功打印 HTML 代码,但并非所有网页。在它不起作用的情况下,我收到以下错误:

Name or service not known

有人可以给我一个解决方案吗?执行getaddrinfo时出现错误。经过广泛搜索答案后,我似乎无法弄清楚。谢谢大家!

我的代码如下:

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdarg.h>

/* "BSIZE" is the size of the buffer we use to read from the socket. */

#define BSIZE 0x1000

/* Quickie function to test for failures. It is actually better to use
   a macro here, since a function like this results in unnecessary
   function calls to things like "strerror". However, not every
   version of C has variadic macros. */

static void fail (int test, const char * format, ...)
{
    //printf("here in fail");
    if (test) {
        va_list args;
        va_start (args, format);
        vfprintf (stderr, format, args);
        va_end (args);
        exit (EXIT_FAILURE);
    }
}

/* Get the web page and print it to standard output. */

static char * get_page (const char * host)
{
    /* Output */
    char * htmlCode;

    const char * page = "momoe/";
    /* "s" is the file descriptor of the socket. */
    int s;    

    struct addrinfo hints, *res, *res0;
    int error;

    memset (&hints, 0, sizeof(hints));
    /* Don't specify what type of internet connection. */
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    error = getaddrinfo (host, "http", & hints, & res0);
    //printf("here 2");
    fail (error, gai_strerror(error));
    //printf("here3");
    s = -1;
    for (res = res0; res; res = res->ai_next) {
        s = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
        fail (s < 0, "socket: %s\n", strerror (errno));
        if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
            fprintf (stderr, "connect: %s\n", strerror (errno));
            close(s);
            exit (EXIT_FAILURE);
        }
        break;
    }
    freeaddrinfo (res0);
    if (s == -1) {
        printf("Error with socket file ID");
        return "Err";
    }

    /* "msg" is the request message that we will send to the
       server. */

    char * msg;

    /* "format" is the format of the HTTP request we send to the web
       server. */

    const char * format =
        "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: fetch.c\r\n\r\n";

    /* This holds return values from functions. */

    int status;

    /* I am using non-standard function "asprintf" for convenience. If
       you don't have "asprintf", use "snprintf" with a fixed-size
       buffer and check its return value against the length of the
       buffer after the call. */

    status = asprintf (& msg, format, page, host);

    /* Check that "asprintf" succeeded. */

    fail (status == -1 || ! msg, "asprintf failed.\n");

    /* Send the request. */

    status = send (s, msg, strlen (msg), 0);

    /* Check it succeeded. The FreeBSD manual page doesn't mention
       whether "send" sets errno, but
       "http://pubs.opengroup.org/onlinepubs/009695399/functions/send.html"
       claims it does. */

    fail (status == -1, "send failed: %s\n", strerror (errno));

    while (1) {
        /* The number of bytes received. */
        int bytes;
        /* Our receiving buffer. */
        char buf[BSIZE+10];
        /* Get "BSIZE" bytes from "s". */
        bytes = recvfrom (s, buf, BSIZE, 0, 0, 0);
        /* Stop once there is nothing left to print. */
        if (bytes == 0) {
            break;
        }
        fail (bytes == -1, "%s\n", strerror (errno));
        /* Nul-terminate the string before printing. */
        buf[bytes] = '\0';
        /* Add buffer text to output. */
        //printf ("%s", buf);
        strncat(htmlCode, buf, strlen(buf));
    }
    free (msg);
    return htmlCode;
}

int main () {
    /* Get one of the web pages here. */
    char * host = "http://xkcd.com/352";
    char * webPage;
    //printf("\nhere\n");
    webPage = get_page (host);
    printf("Print HTML:\n\n%s", webPage);

    return 0;
}

最佳答案

您似乎正在传递“http://xkcd.com/352 ”作为主机名。您需要解析出主机部分并仅传递它,例如“xkcd.com”

关于C 套接字错误 : "Name or service not known",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25519529/

相关文章:

java - 在 ssl 连接中打开 serversocket 后,我​​可以重新加载 trustmanager

c++ - getaddrinfo,我没有得到任何 canonname

c - 我需要遍历 getaddrinfo() 吗?

c - 具有 `addrinfo` 结构的内存管理

linux - 如何以编程方式将程序的输出重定向到 SSL 套接字

使用C中的fork()并行计算文件夹中每个文本文件的行数

c - 为什么我会收到 lValue 错误?

无法在 Centos 中的 FT230X 中设置 GPIO 引脚

C - 通知另一个线程清理并退出

python - 什么是 C 相当于 python 的 map()