I created a C program that will make a connection to a website. I can compile it with no errors at all but when I run it, I still can't create a connection to my specified website. The errors that are shown in the terminal are:
client: connect: Connection timed out
client: connect: Connection timed out
client: failed to connect

Right now I can't find where the error might be. So may I ask anyone if there is something erroneous/missing in the program? Thank you.

My program looks like the one below:

// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) {
         return &(((struct sockaddr_in*)sa)->sin_addr);
    }
    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main()
{
    struct addrinfo hints, *res, *p;
    int sockfd, status;
    char s[INET6_ADDRSTRLEN];

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
    hints.ai_socktype = SOCK_STREAM;

    if ((status = getaddrinfo("www.yahoo.com", "8080", &hints, &res)) != 0) {
	fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
	return 2;
    }

    for (p = res; p != NULL; p = p->ai_next) {
	    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
        	    perror("client: socket");
		    continue;
	    }

    	    if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
        	    close(sockfd);
        	    perror("client: connect");
		    continue;
    	    }
    break;
    }

    if (p == NULL) {
	fprintf(stderr, "client: failed to connect\n");
	return 2;
    }

    inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);
    printf("Successfully connected to: %s.\n", s);

    freeaddrinfo(res);
    close(sockfd);
    return 0;

}

Is the code necessarily needed to be in C??

Yes, because if I'm able to implement socket programming for my purpose (create a connection to a url) I'll still be using the source code to monitor a database created using MySQL.

To give you an overview of the whole system I'm trying to create, the data stored in the database will be monitored by the C program and when one of the data is changed/updated, the program will prompt a connection to a certain website/url. C is the programming language I'm familiar with that I know could do both tasks I've mentioned. I think cURL could also be used to create a connection to a website but I'm not sure if it can also be used for MySQL purposes. If you know of any other programming language more suited for the purposes, it would be a big help.

I suggest you consider either C# or alternatively Python. They act much more efficiently in handling networking and as well as connecting to MySQL.

You can also consider Java, but personally, it's much easier for me to use C# or Python.

I suggest you consider either C# or alternatively Python. They act much more efficiently in handling networking and as well as connecting to MySQL.

You can also consider Java, but personally, it's much easier for me to use C# or Python.

Ok, do I have to install something in Linux to be able to program using C# and python?

As far as I know, C# belongs to Microsoft and you need Windows + MS DotNet.

But for python, it's already installed on Linux so you don't need to install anything except for some good Python editor like Geany.

Look at this page for some tutorials on socket programming on C, Python, Java, Perl.

This article has been dead for over six months. Start a new discussion instead.