error C2065: 'socklen_t' : undeclared iden

2019-06-01 19:13发布

问题:

The whole error is:

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

This is the problematic line:

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

I have these headers included:

#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>

I have also added WS2_32.lib to the linker in Visual Studio 2010.

What else could cause this problem? I'm just trying to rewrite my simple UDP program to work under Windows.

回答1:

The socklen_t type is defined inside of WS2tcpip.h in windows. This is not transitively included from winsock2.h (AFAICT). You'll need to include WS2tcpip.h manually in order to use the socklen_t type.



回答2:

Visual Studio can't find the type socklen_t. MSDN says that this function takes an int* as the last parameter, so cast to that.