I'm using g++ 4.9.2
compiler and wrote the following code to try my first example with sockets.
char *buf = new char[1000];
int iResult;
int main(){
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
addrinfo hints, *result;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
std::cout << WSAStartup(MAKEWORD(2,2), &wsaData) << std::endl;
iResult = getaddrinfo("173.194.40.231", "80", &hints, &result);
std::cout << iResult;
ConnectSocket = socket(result -> ai_family, result -> ai_socktype, result -> ai_protocol); //1, HERE
connect(ConnectSocket, result->ai_addr, result->ai_addrlen);
}
The issue, that I got Segmentation fault
at 1
. Because of getaddrinfo("173.194.40.231", "80", &hints, &result);
returned 11003
, therefore &result
pointed to NULL
. Why? I typed valid address and port.
It was google.com
and we can open up it in browser. Couldn't someone help me out?