0

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?

4
  • 1
    Right-click the source file and find the menu item that has to do with normalizing line endings. See this answer for info on what that means. The issue usually happens when the IDE can't match the execution point with the source code due to mismatched line endings. Commented May 15, 2014 at 17:44
  • @KenWhite Where do I find that menu item or how is it called exactly? I can't seem to find it. Commented May 15, 2014 at 17:48
  • @KenWhite I found it in advanced save options. Thanks for the help, it works now. Commented May 15, 2014 at 17:55
  • You found it quicker than I could see your comment and open VS. :-) Commented May 15, 2014 at 18:02

1 Answer 1

1

This is usually caused by mismatched line endings (some lines ending with a CR/LF, some ending with just a CR or LF), which causes the IDE not to be able to keep the code editor and debugger in synch.

VS often recognizes this condition and displays a dialog asking about whether you want to normalize line endings (see What does Visual Studio mean by normalizing line endings? for more information).

You can fix this yourself by using the File->Advanced Save Options menu item, and setting the Line Endings to Windows (CR LF).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.