0

I have made a pretty simple http client which would retrieve html from http website and print it. But there seems to be some problem while using connect() function.Using perror() I found that it is giving connection refused error.

This is my code

#include <string.h>
#include <stdio.h>
#include <iostream>

#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>

#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
    char *address;
    address = argv[1];

    int c_socket = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in URLaddress;

    URLaddress.sin_family = AF_INET;
    URLaddress.sin_port = htons(80);
    inet_pton(0,address,&URLaddress.sin_addr.s_addr);
    int con = connect(c_socket, (struct sockaddr*) &URLaddress, 
    sizeof(URLaddress));

    perror("error");

    return 0;
}

This is the input

151.101.13.5

This is the output

error: Connection refused

I am passing IP of website as input.

I have seen all other similar questions but didn't get any answer as how to fix this because this keeps happening with every website I try my program with. Please tell how to resolve this.

0

2 Answers 2

1

The following version of the code works on Windows/MSC:

int test(void)
{
    char *address;
    int c_socket, con;
    struct sockaddr_in URLaddress;
    char request[] = "GET / HTTP/1.1\r\n\r\n";
    char response[4096];
    WORD wVersionRequested;
    WSADATA wsaData;

    wVersionRequested = MAKEWORD( 2, 2 );

    address = "151.101.13.5";

    if (WSAStartup (wVersionRequested, &wsaData)!= 0) {
        printf("DLL not found\n");
        return -1;
    }
    if ((c_socket = socket(AF_INET, SOCK_STREAM, 0)) ==  INVALID_SOCKET ) {
        printf("socket error: %d\n", WSAGetLastError());
        return -1;
    }

    URLaddress.sin_family = AF_INET;
    URLaddress.sin_port = htons(80);
    URLaddress.sin_addr.s_addr= inet_addr(address);
    con = connect(c_socket, (struct sockaddr*) &URLaddress, sizeof(URLaddress));

    send(c_socket, request, strlen(request), 0);
    recv(c_socket, response, sizeof(response), 0);
    WSACleanup();

    printf ("%s\n",response);

    return 0;
}

The repsonse is:

<title>Fastly error: unknown domain </title>
</head>
<body>
<p>Fastly error: unknown domain: . Please check that this domain has been added to a service.</p>
<p>Details: cache-fra19128-FRA</p></body></html>5¸`¡qu
Sign up to request clarification or add additional context in comments.

2 Comments

although i am on linux mint but i have tried changing this line inet_pton(0,address,&URLaddress.sin_addr.s_addr); with //inet_pton(0,address,&URLaddress.sin_addr.s_addr); and it is now at least connecting and giving some sort of output error: Success GET / HTTP/1.1 which is still not satisfactory but better than before.Thanks
Success as the output of perror means there is no error. Did you print response?
0

inet_pton(3) requires a first parameter to specify the address family and you have passed 0 for it (which is not the same as AF_INET), and you had to pass AF_INET, in accordance of the protocol family you are using.

inet_pton(3) is a protocol family independent conversion routine, but it needs to know what is the actual used to be able to convert addresses properly.

By the way, is a server listening on the requested address and port? have you tested that a browser is capable of getting something from that address before running your program?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.