I am trying to connect to a server that I create. My server works fine if the client is in the same host because I use this in the client code:
Code:
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
Now, when I try to connect to the server, being in a different host, I do this:
Code:
struct hostent *he;
..
char *servername = argv[1];
he = gethostbyname(servername);
memcpy(&serverAddr.sin_addr, he->h_addr_list[0], he->h_length);
// Connecting to the server
puts("1");
if (connect(mySock, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) {
puts("2");
perror("connect");
return EXIT_FAILURE;
}
puts("Connected to the server.");
The problem is that the "1" gets printed, and the "Connected..." message never appears.. So it gets stuck in connect. I am 100% sure it's something wrong with that gethostbyname, I must be using it wrong.
Can anyone enlighten me?