【发布时间】:2014-05-15 17:41:42
【问题描述】:
我的控制台应用程序有问题。我正在学习某种关于网络的教程,当我尝试在调试中运行时,我遇到了一个以前从未见过的奇怪的运行时错误。
当我在 main 函数内的第一个新行上放置一个断点并使用 Step Over (F10) 遍历代码时,Visual Studio 执行包括 WSAStartup() 在内的前 3 行代码,然后突然到达一个注释部分:
#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。 更具体地说,它跳转到:
#ifdef WPRFLAG
__winitenv = envp;
mainret = wmain(argc, argv, envp);
#else /* WPRFLAG */
__initenv = envp;
mainret = main(argc, argv, envp); //here
然后到:
#else /* !defined (_WINMAIN_) && defined (_CRT_APP) */
if ( !managedapp )
{
#ifndef _CRT_APP
exit(mainret); //here
我已经尝试摆脱所有的 cmets,当我在调试中运行时,代码的行为会有所不同(但实际上并不像预期的那样)。
这里到底发生了什么,我该如何解决?
【问题讨论】:
-
右键单击源文件并找到与规范化行尾有关的菜单项。有关这意味着什么的信息,请参阅this answer。当 IDE 由于行尾不匹配而无法将执行点与源代码匹配时,通常会出现此问题。
-
@KenWhite 我在哪里可以找到该菜单项,或者它的确切名称是什么?好像没找到。
-
@KenWhite 我在高级保存选项中找到了它。感谢您的帮助,现在可以使用了。
-
你发现它的速度比我看到你的评论和打开 VS 的速度还要快。 :-)
标签: visual-c++ visual-studio-2013