C Socket Error: “Name or service not known”

a 夏天 提交于 2019-12-22 15:59:13

问题


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

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!

My code is as follows:

#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;
}

回答1:


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"




回答2:


There are mainly 3 issues in your code,

1. /* Output */
   char * htmlCode;

You are not allocating memory for htmlCode variable anywhere in the program.

Ex: char htmlCode[1024*60];

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

Page name you given is not in the proper format.

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

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

The value in host variable is in wrong format.

Ex: char *host = "google.com"

You change the program and execute it. It'll work.



来源:https://stackoverflow.com/questions/25519529/c-socket-error-name-or-service-not-known

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!