I have a problem with my console application. I'm following some kind of a tutorial on networking, and when I try to run in debug I have a weird runtime error I have never seen before.
When I put a breakpoint on the first new line inside the main function and go through the code with Step Over (F10), visual studio executes the first ~3 lines of code including WSAStartup(), then suddenly reaches acomment section:
#include <winsock2.h> #include <WS2tcpip.h> #include <iostream> #include "wsh_includes.h" int main(int argc, const char* argv[]) { //You have to make a call to WSAStartup() before doing anything else with the sockets library //WSADATA wsaData; // if this doesn't work WSAData wsaData; // then try this instead wsh::errcheck(WSAStartup(MAKEWORD(2, 2), &wsaData), "WSAStartup failed"); //---------- //You also have to tell your compiler to link in the Winsock library, usually called //wsock32.lib or winsock32.lib, or ws2_32.lib for Winsock 2.0 //---------- //you can't use close() to close a socket—you need to use closesocket() //---------- //select() only works with socket descriptors, not file descriptors (like 0 for stdin). //There is also a socket class that you can use, CSocket //---------- int status; addrinfo hints, *res, *p; char ipstr[INET6_ADDRSTRLEN]; memset(&hints, 0, sizeof(hints)); //make sure it's empty hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version hints.ai_socktype = SOCK_STREAM; //TCP stream sockets status = getaddrinfo("www.example.net", NULL, &hints, &res); wsh::errcheck(status, "getaddrinfo failed"); //servinfo now points to a linked list of 1 or more struct addrinfos //... do everything until you don't need servinfo anymore ... printf("IP addresses for %s:\n\n", argv[1]); for (p = res; p != NULL; p = p->ai_next) { void* addr; char* ipver; //get the pointer to the address itself //different fields in IPv4 and IPv6: if (p->ai_family == AF_INET) { sockaddr_in* ipv4 = (sockaddr_in*)p->ai_addr; addr = &(ipv4->sin_addr); ipver = "IPv4"; } else { sockaddr_in6* ipv6 = (sockaddr_in6*)p->ai_addr; addr = &(ipv6->sin6_addr); ipver = "IPv6"; } //convert the IP to a string and print it: inet_ntop(p->ai_family, addr, ipstr, sizeof(ipstr)); printf(" %s: %s\n", ipver, ipstr); } std::cin.get(); freeaddrinfo(res); //free the linked list //---------- //Finally, you need to call WSACleanup() when you're all through with the sockets library. wsh::errcheck(WSACleanup(), "WSACleanup failed"); return 0; } When it gets there, it suddenly moves to the file crtexe.c in the middle of the commented section. More specifically, it jumps to:
#ifdef WPRFLAG __winitenv = envp; mainret = wmain(argc, argv, envp); #else /* WPRFLAG */ __initenv = envp; mainret = main(argc, argv, envp); //here then to:
#else /* !defined (_WINMAIN_) && defined (_CRT_APP) */ if ( !managedapp ) { #ifndef _CRT_APP exit(mainret); //here I have tried getting rid of all the comments, when I run in debug then the code will behave differently (yet not really as expected).
What exactly is going on here and how can I fix it?