错误C2065:'socklen_t':未声明的标识符

时间:2010-12-15 19:27:45

标签: c++ visual-c++ socklen-t

整个错误是:

Error   1   error C2065: 'socklen_t' : undeclared identifier    c:\users\richard\documents\visual studio 2010\projects\server\server\server.cpp 41  1   Server

这是有问题的一行:

int iRcvdBytes=recvfrom(iSockFd, buff, 1024000, 0, (struct sockaddr*)&cliAddr, (socklen_t*)&cliAddrLen);

我有这些标题:

#include <winsock2.h>
#include <windows.h>

#include <direct.h>
#include <stdlib.h>
#include <stdio.h>

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

我还在Visual Studio 2010中将WS2_32.lib添加到链接器。

还有什么可能导致这个问题?我只是想重写我的简单UDP程序在Windows下工作。

2 个答案:

答案 0 :(得分:14)

socklen_t类型在Windows中的WS2tcpip.h中定义。这不是来自winsock2.h(AFAICT)的传递。您需要手动包含WS2tcpip.h才能使用socklen_t类型。

答案 1 :(得分:1)

Visual Studio找不到类型socklen_t。 MSDN说这个函数将int *作为最后一个参数,所以强制转换为。