首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >控制台应用程序运行时问题

控制台应用程序运行时问题
EN

Stack Overflow用户
提问于 2014-05-15 17:41:42
回答 1查看 1.1K关注 0票数 0

我的控制台应用程序有问题。我正在学习某种关于网络的教程,当我尝试在调试中运行时,我有一个以前从未见过的奇怪的运行时错误。

当我在主函数中的第一个新行上放置一个断点并使用Step Over (F10)遍历代码时,visual将执行包括WSAStartup()在内的前3行代码,然后突然到达acomment部分:

代码语言:javascript
复制
#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;
}

当它到达那里时,它突然移动到注释部分中间的crtexe.c文件。更具体地说,它跳到:

代码语言:javascript
复制
#ifdef WPRFLAG
            __winitenv = envp;
            mainret = wmain(argc, argv, envp);
#else  /* WPRFLAG */
            __initenv = envp;
            mainret = main(argc, argv, envp); //here

然后转到:

代码语言:javascript
复制
#else  /* !defined (_WINMAIN_) && defined (_CRT_APP) */
            if ( !managedapp )
            {
#ifndef _CRT_APP
                exit(mainret); //here

我已经尝试过去掉所有的注释,当我在调试中运行时,代码的行为会有所不同(但并不像预期的那样)。

到底是怎么回事,我该怎么解决呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-15 18:03:59

这通常是由不匹配的行尾引起的(有些行以CR/LF结尾,有些以CR或LF结尾),这导致IDE无法保持代码编辑器和调试器同步。

VS经常识别这种情况,并显示一个对话框,询问是否要使行尾正常化(更多信息请参见What does Visual Studio mean by normalizing line endings? )。

您可以通过使用File->Advanced 菜单项并将LineEndings设置为 (CR )来自己解决这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23685191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档