C套接字错误:“名称或服务未知" [英] C Socket Error: "Name or service not known"

查看:117
本文介绍了C套接字错误:“名称或服务未知"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C语言编写一个使用套接字来获取网页的程序.我的代码当前可以在某些网页上成功打印HTML代码,但并非在所有网页上都能成功打印.在无法正常运行的情况下,会出现以下错误:

I'm trying to code a program in C that uses sockets to fetch webpages. My code currently prints successfully the HTML code at some webpages, but not all webpages. In the instances where it does not work I get the following error:

名称或服务未知

Name or service not known

有人可以为我提供解决方案吗?执行getaddrinfo时发生错误.经过广泛搜索答案后,我似乎无法弄清楚.谢谢大家!

Can someone please offer me a solution? The error occurs when executing getaddrinfo. I can't seem to figure it out after an extensive search for the answer. Thanks everyone!

我的代码如下:

#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"

It looks like you're passing "http://xkcd.com/352" as the host name. You'll need to parse out the host part and only pass it, eg "xkcd.com"

这篇关于C套接字错误:“名称或服务未知"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆