C++ 套接字错误无法在 WinSock2.h 中获取函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22517036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
socket errors can't get functions in WinSock2.h
提问by David Heffernan
I have created a socket wrapper class but I have too many errors that I cannot figure out how to solve. I tried Winsock tutorial errorand Can't include Winsock2.h in MSVC 2010, but no use
我创建了一个套接字包装类,但我有太多的错误,我无法弄清楚如何解决。我尝试了Winsock 教程错误并且无法在 MSVC 2010 中包含 Winsock2.h,但没有用
Here is the code :
这是代码:
//#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <string>
#include <Windows.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#pragma comment(lib,"ws2_32_lib")
using namespace std;
#define WM_SOCKET 0x10000
class Socket
{
};
Here are the errors:
以下是错误:
Error 107 error C3861: 'WSAStartup': identifier not found 1 Project2_SocketLib
Error 89 error C3861: 'WSASetLastError': identifier not found C:\Program Files (x86)\Windows Kits.0\Include\um\WS2tcpip.h 696 1 Project2_SocketLib
Error 90 error C3861: 'WSASetLastError': identifier not found C:\Program Files (x86)\Windows Kits.0\Include\um\WS2tcpip.h 703 1 Project2_SocketLib
Error 91 error C3861: 'WSASetLastError': identifier not found C:\Program Files (x86)\Windows Kits.0\Include\um\WS2tcpip.h 742 1 Project2_SocketLib
Error 92 error C3861: 'WSASetLastError': identifier not found C:\Program Files (x86)\Windows Kits.0\Include\um\WS2tcpip.h 749 1 Project2_SocketLib
Error 93 error C3861: 'WSASetLastError': identifier not found C:\Program Files (x86)\Windows Kits.0\Include\um\WS2tcpip.h 793 1 Project2_SocketLib
Error 94 error C3861: 'WSASetLastError': identifier not found C:\Program Files (x86)\Windows Kits.0\Include\um\WS2tcpip.h 800 1 Project2_SocketLib
Error 95 error C3861: 'WSASetLastError': identifier not found C:\Program Files (x86)\Windows Kits.0\Include\um\WS2tcpip.h 841 1 Project2_SocketLib
Error 96 error C3861: 'WSASetLastError': identifier not found C:\Program Files (x86)\Windows Kits.0\Include\um\WS2tcpip.h 848 1 Project2_SocketLib
Error 109 error C3861: 'WSAGetLastError': identifier not found 1 Project2_SocketLib
Error 112 error C3861: 'WSAGetLastError': identifier not found 1 Project2_SocketLib
Error 105 error C3861: 'WSACleanup': identifier not found 1 Project2_SocketLib
Error 103 error C3861: 'shutdown': identifier not found 1 Project2_SocketLib
Error 100 error C3861: 'send': identifier not found 1 Project2_SocketLib
Error 101 error C3861: 'send': identifier not found 1 Project2_SocketLib
Error 98 error C3861: 'recv': identifier not found 1 Project2_SocketLib
Error 99 error C3861: 'recv': identifier not found 1 Project2_SocketLib
Error 116 error C3861: 'inet_ntoa': identifier not found 5 1 Project2_SocketLib
Error 119 error C3861: 'htons': identifier not found 7 1 Project2_SocketLib
Error 126 error C3861: 'htonl': identifier not found 9 1 Project2_SocketLib
Error 104 error C3861: 'closesocket': identifier not found 1 Project2_SocketLib
Error 102 error C3861: 'accept': identifier not found 1 Project2_SocketLib
Error 6 error C3805: 'constant': unexpected token, expected either '}' or a ',' C:\Program Files (x86)\Windows Kits.0\Include\shared\ws2def.h 414 1 Project2_SocketLib
Error 108 error C2568: '+' : unable to resolve function overload 1 Project2_SocketLib
Error 111 error C2568: '+' : unable to resolve function overload 1 Project2_SocketLib
Error 124 error C2568: ':' : unable to resolve function overload 9 1 Project2_SocketLib
Error 125 error C2446: ':' : no conversion from 'unsigned long (__stdcall *)(const char *)' to 'u_long (__stdcall *)(u_long)' 9 1 Project2_SocketLib
Error 106 error C2440: '=' : cannot convert from 'WSADATA' to 'int' 1 Project2_SocketLib
Error 110 error C2440: '=' : cannot convert from 'SOCKET (__stdcall *)(int,int,int)' to 'SOCKET' 1 Project2_SocketLib
Error 115 error C2440: '=' : cannot convert from 'char *(__stdcall *)(in_addr)' to 'std::string' 5 1 Project2_SocketLib
Error 66 error C2375: 'WSAUnhookBlockingHook' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2383 1 Project2_SocketLib
Error 61 error C2375: 'WSAStartup' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2296 1 Project2_SocketLib
Error 63 error C2375: 'WSASetLastError' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2332 1 Project2_SocketLib
Error 67 error C2375: 'WSASetBlockingHook' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2400 1 Project2_SocketLib
Error 65 error C2375: 'WSAIsBlocking' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2366 1 Project2_SocketLib
Error 64 error C2375: 'WSAGetLastError' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2349 1 Project2_SocketLib
Error 62 error C2375: 'WSACleanup' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2315 1 Project2_SocketLib
Error 68 error C2375: 'WSACancelBlockingCall' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2417 1 Project2_SocketLib
Error 75 error C2375: 'WSACancelAsyncRequest' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2592 1 Project2_SocketLib
Error 76 error C2375: 'WSAAsyncSelect' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2612 1 Project2_SocketLib
Error 70 error C2375: 'WSAAsyncGetServByPort' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2466 1 Project2_SocketLib
Error 69 error C2375: 'WSAAsyncGetServByName' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2439 1 Project2_SocketLib
Error 72 error C2375: 'WSAAsyncGetProtoByNumber' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2517 1 Project2_SocketLib
Error 71 error C2375: 'WSAAsyncGetProtoByName' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2492 1 Project2_SocketLib
Error 73 error C2375: 'WSAAsyncGetHostByName' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2542 1 Project2_SocketLib
Error 74 error C2375: 'WSAAsyncGetHostByAddr' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2569 1 Project2_SocketLib
Error 53 error C2375: 'socket' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2120 1 Project2_SocketLib
Error 52 error C2375: 'shutdown' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2099 1 Project2_SocketLib
Error 51 error C2375: 'setsockopt' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2077 1 Project2_SocketLib
Error 50 error C2375: 'sendto' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2051 1 Project2_SocketLib
Error 49 error C2375: 'send' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2026 1 Project2_SocketLib
Error 48 error C2375: 'select' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2002 1 Project2_SocketLib
Error 47 error C2375: 'recvfrom' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1976 1 Project2_SocketLib
Error 46 error C2375: 'recv' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1951 1 Project2_SocketLib
Error 45 error C2375: 'ntohs' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1931 1 Project2_SocketLib
Error 44 error C2375: 'ntohl' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1914 1 Project2_SocketLib
Error 43 error C2375: 'listen' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1896 1 Project2_SocketLib
Error 35 error C2375: 'ioctlsocket' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1658 1 Project2_SocketLib
Error 42 error C2375: 'inet_ntoa' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1795 1 Project2_SocketLib
Error 41 error C2375: 'inet_addr' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1778 1 Project2_SocketLib
Error 40 error C2375: 'htons' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1761 1 Project2_SocketLib
Error 39 error C2375: 'htonl' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1744 1 Project2_SocketLib
Error 38 error C2375: 'getsockopt' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1723 1 Project2_SocketLib
Error 37 error C2375: 'getsockname' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1700 1 Project2_SocketLib
Error 57 error C2375: 'getservbyport' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2221 1 Project2_SocketLib
Error 58 error C2375: 'getservbyname' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2240 1 Project2_SocketLib
Error 59 error C2375: 'getprotobynumber' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2258 1 Project2_SocketLib
Error 60 error C2375: 'getprotobyname' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2275 1 Project2_SocketLib
Error 36 error C2375: 'getpeername' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1679 1 Project2_SocketLib
Error 56 error C2375: 'gethostname' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2181 1 Project2_SocketLib
Error 55 error C2375: 'gethostbyname' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2163 1 Project2_SocketLib
Error 54 error C2375: 'gethostbyaddr' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 2144 1 Project2_SocketLib
Error 34 error C2375: 'connect' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1637 1 Project2_SocketLib
Error 33 error C2375: 'closesocket' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1618 1 Project2_SocketLib
Error 32 error C2375: 'bind' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1599 1 Project2_SocketLib
Error 31 error C2375: 'accept' : redefinition; different linkage C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 1577 1 Project2_SocketLib
Error 118 error C2228: left of '.sin_port' must have class/struct/union 7 1 Project2_SocketLib
Error 120 error C2228: left of '.sin_family' must have class/struct/union 8 1 Project2_SocketLib
Error 121 error C2228: left of '.sin_addr' must have class/struct/union 9 1 Project2_SocketLib
Error 122 error C2228: left of '.S_un' must have class/struct/union 9 1 Project2_SocketLib
Error 123 error C2228: left of '.S_addr' must have class/struct/union 9 1 Project2_SocketLib
Error 114 error C2227: left of '->sin_addr' must point to class/struct/union/generic type 5 1 Project2_SocketLib
Error 97 error C2079: 'Socket::wsaData' uses undefined struct 'WSAData' 1 Project2_SocketLib
Error 117 error C2079: 'sockAddrIn' uses undefined struct 'sockaddr_in' 5 1 Project2_SocketLib
Error 78 error C2079: 'sockaddr_gen::AddressIn' uses undefined struct 'sockaddr_in' C:\Program Files (x86)\Windows Kits.0\Include\shared\ws2ipdef.h 81 1 Project2_SocketLib
Error 77 error C2079: 'sockaddr_gen::Address' uses undefined struct 'sockaddr' C:\Program Files (x86)\Windows Kits.0\Include\shared\ws2ipdef.h 80 1 Project2_SocketLib
Error 87 error C2079: '_SOCKADDR_INET::Ipv4' uses undefined struct 'sockaddr_in' C:\Program Files (x86)\Windows Kits.0\Include\shared\ws2ipdef.h 202 1 Project2_SocketLib
Error 5 error C2059: syntax error : 'constant' C:\Program Files (x86)\Windows Kits.0\Include\shared\ws2def.h 414 1 Project2_SocketLib
Error 113 error C2027: use of undefined type 'sockaddr_in' 5 1 Project2_SocketLib
Error 21 error C2011: 'WSAData' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 353 1 Project2_SocketLib
Error 16 error C2011: 'timeval' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 169 1 Project2_SocketLib
Error 22 error C2011: 'sockproto' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 451 1 Project2_SocketLib
Error 12 error C2011: 'sockaddr_in' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\shared\ws2def.h 608 1 Project2_SocketLib
Error 4 error C2011: 'sockaddr' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\shared\ws2def.h 217 1 Project2_SocketLib
Error 19 error C2011: 'servent' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 245 1 Project2_SocketLib
Error 20 error C2011: 'protoent' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 257 1 Project2_SocketLib
Error 18 error C2011: 'netent' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 238 1 Project2_SocketLib
Error 23 error C2011: 'linger' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 493 1 Project2_SocketLib
Error 88 error C2011: 'ip_mreq' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\shared\ws2ipdef.h 715 1 Project2_SocketLib
Error 17 error C2011: 'hostent' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 225 1 Project2_SocketLib
Error 13 error C2011: 'fd_set' : 'struct' type redefinition C:\Program Files (x86)\Windows Kits.0\Include\um\WinSock2.h 125 1 Project2_SocketLib
Error 127 error C1003: error count exceeds 100; stopping compilation 9 1 Project2_SocketLib
回答by David Heffernan
You need to re-order the includes of the Windows headers like so:
您需要重新排序 Windows 标头的包含内容,如下所示:
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <Windows.h>
In other words, the WinSock headers must be included before Windows.h
.
换句话说,WinSock 标头必须包含在Windows.h
.
Or define WIN32_LEAN_AND_MEAN
which you have commented out for some reason.
或者定义WIN32_LEAN_AND_MEAN
您出于某种原因注释掉的内容。
I think you will also need to change the Accept
method to return SOCKET
rather than Socket
.
我认为您还需要将Accept
方法更改为 returnSOCKET
而不是Socket
.
And the #pragma
should be:
而#pragma
应该是:
#pragma comment(lib,"ws2_32")
回答by MicroVirus
You can't include Windows.h before you include WinSock2, as this will lead the windows header to include an older version of winsock. Remove the Windows.h include altogether.
在包含 WinSock2 之前不能包含 Windows.h,因为这将导致 windows 标头包含旧版本的 winsock。完全删除 Windows.h 包含。
See, for instance, this MSDN link: http://msdn.microsoft.com/en-us/library/windows/desktop/ms737629(v=vs.85).aspxThe Note part under the first code block explains this.
例如,请参阅此 MSDN 链接:http: //msdn.microsoft.com/en-us/library/windows/desktop/ms737629( v= vs.85).aspx第一个代码块下的注释部分对此进行了解释。
回答by kiriloff
I made sure that an #include "Winsock2.h" is before any #include "windows.h" and "#include "Winsock.h" and this solved the case.
我确保 #include "Winsock2.h" 在任何 #include "windows.h" 和 "#include "Winsock.h" 之前,这解决了这个问题。
Just a matter of patience, look at includes one by one and establish this order, first #include "Winsock2.h" then #include "windows.h"
耐心点,一一查看includes,建立这个顺序,先#include "Winsock2.h" 然后#include "windows.h"
I checked the recursive includes, I spotted the header files which include (recursively) some #include "windows.h" and "#include "Winsock.h"and write a#include "Winsock2.h". in this files, i added#include "Winsock2.h"` as the first include.
我检查了递归包含,我发现了头文件,其中包含(递归地)一些 #include "windows.h" 和 "#include "Winsock.h" 并写了一个#include "Winsock2.h"。在这个文件中,我添加了#include "Winsock2.h"` 作为第一个包含。