console application runtime problems

xcrypt

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?

Ken White

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

console application runtime problems

From Dev

Qt console application problems when reading input

From Dev

Enumerating runtime services in an ASP.NET5 Console Application

From Dev

No Worklight 6.2 Runtime in Console

From Dev

Delphi XE8: problems running an external console application, waiting for its results and capturing its results

From Dev

Problems using HTTParty in console

From Dev

problems with js console output

From Dev

Assembly basic calculator runtime problems

From Dev

No Runtime can be found in Worklight Console

From Dev

No runtime on my Worklight 6.2 Console

From Dev

Change Application MainForm at runtime

From Dev

Runtime configuration for Symfony application

From Dev

Change Application MainForm at runtime

From Dev

Problems with Encoding in Eclipse Console and Python

From Dev

Problems with Compiling Play Application

From Dev

Problems with extending the polls application

From Dev

Problems with my winsock application

From Dev

Untrusted Application Problems

From Dev

Start WPF Application in Console Application

From Dev

Creating console MonoMac application

From Java

Display a Image in a console application

From Java

Progress bar in console application

From Dev

What is a vNext console application?

From Dev

Make a console application into a service?

From Dev

Color in Console Application

From Dev

Console Application Code Error

From Dev

Spring + Hibernate console application

From Dev

Console Application with task scheduler

From Dev

AvailableVirtualMemory on IIS and Console application