getaddrinfo ()
does not translate a hostname into an IP address and as a result Connect to server ()
Is there some discrepancy in my implementation - Not compiled with a warning message?
Is this function called call connect
correct?
connect (client, result -> ai_addr, result-> ai_addrlen)
The full implementation listed below:
#include & lt; Winsock2.h & gt; # Include & lt; Ws2tcpip.h & gt; #include & lt; Windows.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Iostream & gt; #pragma comment (lib, "ws2_32.lib") using namespace std; Int main (int argc, char * argv []) {if (argc! = 3) {cerr & lt; & Lt; "Use:" & lt; & Lt; Argv [0] & lt; & Lt; "[Hostname] [Port number] \ n"; Exhaust (EXIT_FAILURE); } WSADATA wsaData; Word wVersionRequested; Int wError; WVersionRequested = Macward (2, 2); WError = WSAStartup (wVersionRequested, and wsaData); If (wError! = 0) {cerr & lt; & Lt; "Failed with WSAStartup error:" & lt; & Lt; Wehr & lt; & Lt; Endl; Exit (EXIT_FAILURE); } / * * Confirm that WinSock supports DLL 2.2. * Note that if DLL supports more than 2.2 versions in addition to 2.2, then it will still return to * 2.2 in Warrison because we've requested * that version. * / If (LBYT (wsaData.wVersion)! = 2 || Hbbet (wsaData.wVersion)! = 2) {cerr & lt; & Lt; "The version using Winsock.dll could not be found." & Lt; & Lt; Endl; WSACleanup (); Exhaust (EXIT_FAILURE); } And {cout & lt; & Lt; "Winsock 2.2 DLL was found." & Lt; & Lt; Endl; } Socket Client; If ((client = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR {cerr & lt; & Lt; "Error: socket () return value == SOCKET_ERROR" & lt; & Lt; Endl; WSACleanup (); Exit (EXIT_FAILURE); } Cout & lt; & Lt; "Socket made." & Lt; & Lt; Endl; Struct addrinfo * result = null; Straight addrinfo signal; Membrane (and hint, 0, size (hint)); Signal Aai_family = AF_INET; Signal Aai_socktype = SOCK_STREAM; Hint .ai_protocol = IPPROTO_TCP; If ((wError = getaddrinfo (argv [1], argv [2], and hints, and result) = 0) {freeaddrinfo (result); WSACleanup (); If (wError == 11001) {cerr & lt; & Lt; "Error: occurred: getaddrinfo () failed" & lt; & Lt; Wehr & lt; & Lt; " - host not found." & Lt; & Lt; Endl; Exhaust (EXIT_FAILURE); } CRR & LT; & Lt; "Error: occurred: getaddrinfo () failed" & lt; & Lt; Wehr & lt; & Lt; Endl; Exhaust (EXIT_FAILURE); } / * * Attempt to connect to server * * / switch (wError = connect (client, result-> AI_ADD, result-> AI_Edryl)) {Case 0: Ser. & Lt; "Hostname Hull." & Lt; & Lt; Endl; break; Case SOCKET_ERROR: wError = WSAGetLastError (); Serie Lieutenant; & Lt; "Error: Connaught () Failed" "Description:" & lt; & Lt; Wehr & lt; & Lt; Endl; Closesocket (customer); Freeaddrinfo (results); WSACleanup (); Exhaust (EXIT_FAILURE); break; Default: cerr & lt; & Lt; "Fatal connect () error: unexpected return value." & Lt; & Lt; Endl; Closesocket (customer); Freeaddrinfo (results); WSACleanup (); Exhaust (EXIT_FAILURE); break; } Cout & lt; & Lt; "Connected to server." & Lt; & Lt; Endl; Closesocket (customer); Freeaddrinfo (results); WSACleanup (); Exits (EXIT_SUCCESS); }
getaddrinfo
can give you an IPv6 address, Or maybe there are more than one IP address in the machine and you are trying to connect to the wrong.
In addition, if your server is listening to 127.0.0.1 and you try to connect to the real IP address, the connection will fail similarly, if the server is listening on the real IP address and When you try to connect to using 127.0.0.1, the connection will fail. If the server listens at 0.0.0.0, then both addresses should work.
To listen to 0.0.0.0, you will have a code like this:
sockaddr_in sin; Sin.sin_family = AF_INET; Sin.sin_addr.s_addr = INADDR_ANY; Sin.sin_port = htons (port_num); Dam (s, (sokaid * *) and sin, shape (sin));
Comments
Post a Comment