读书人

100分怎么正确包含 winsock2.h

发布时间: 2012-02-11 09:51:34 作者: rapoo

100分求助,如何正确包含 winsock2.h
我包含了这个就出错,从网上搜了一个winsocki.h,内容如下:
//
// winsock2i.h - Include winsock2.h safely.
//
// Copyleft 02/24/2005 by freefalcon
//
//
// When WIN32_LEAN_AND_MEAN is not defined and _WIN32_WINNT is LESS THAN 0x400,
// if we include winsock2.h AFTER windows.h or winsock.h, we get some compiling
// errors as following:
// winsock2.h(99) : error C2011: 'fd_set ' : 'struct ' type redefinition
//
// When WIN32_LEAN_AND_MEAN is not defined and _WIN32_WINNT is NOT LESS THAN 0x400,
// if we include winsock2.h BEFORE windows.h, we get some other compiling errors:
// mswsock.h(69) : error C2065: 'SOCKET ' : undeclared identifier
//
// So, this file is used to help us to include winsock2.h safely, it should be
// placed before any other header files.
//


#ifndef _WINSOCK2API_

// Prevent inclusion of winsock.h
#ifdef _WINSOCKAPI_
#error Header winsock.h is included unexpectedly.
#endif

// NOTE: If you use Windows Platform SDK, you should enable following definition:
// #define USING_WIN_PSDK

#if !defined(WIN32_LEAN_AND_MEAN) && (_WIN32_WINNT > = 0x0400) && !defined(USING_WIN_PSDK)
#include <windows.h>
#else
#include <winsock2.h>
#endif

#endif//_WINSOCK2API_


这个可以用在debug办,但是release版不行。
不知道怎么解决,急问,请各位帮忙。

[解决办法]
在你的stdafx.h里面包含一次
#include "winsock2.h "
#pragma comment (lib, "ws2_32.lib)

有#include <afxsock.h> 的话删除掉

其他类就不用包含了 也不行么? 你的错误提示是什么?
[解决办法]
#include <Winsock2.h>
#pragma comment(lib, "ws2_32.lib ")
[解决办法]
#include <Winsock2.h>
#pragma comment(lib, "ws2_32.lib ")
[解决办法]

#include <Winsock2.h>
#pragma comment(lib, "ws2_32.lib ")

就行了。

不要 include windows.h
[解决办法]
winsock2.h要包含在windows.h之前
例如
#include <winsock2.h>
#include <windows.h>
如果是
#include <windows.h>
#include <winsock2.h>
就会报错

读书人网 >VC/MFC

热点推荐