【问题标题】:C Socket Error: "Name or service not known"C 套接字错误:“名称或服务未知”
【发布时间】:2014-08-27 05:28:59
【问题描述】:

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

名称或服务未知

有人可以给我一个解决方案吗?执行 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;
}

【问题讨论】:

  • 您在哪个平台上工作?
  • 来自getaddrinfo 的错误,听起来您的 IP 地址无效。打印您传递给getaddrinfo 的地址以获取错误信息。

标签: c sockets getaddrinfo


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你的代码主要有3个问题,

    1. /* Output */
       char * htmlCode;
    

    您没有在程序中的任何位置为htmlCode 变量分配内存。

    Ex: char htmlCode[1024*60];

    2. const char * page = "momoe/";
    

    您提供的页面名称格式不正确。

    Ex: const char* page = "Homepage.html"

    3. char * host = "http://xkcd.com/352";
      webPage = get_page (host);
    

    host 变量中的值格式错误。

    Ex: char *host = "google.com"

    您更改程序并执行它。它会起作用的。

    【讨论】:

      猜你喜欢
      • 2015-01-05
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 2013-07-24
      相关资源
      最近更新 更多