Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

c - initialize winsock without wsadata using WSAStartup?

Basically I wanted to know if you can initialize a winsock without creating WSADATA variable or feeding the WSADATA value directly to WSAStartup function?

I tried :

WSAStartup(0x202, NULL);
WSAStartup(0x202, (WSADATA *)0x00);

with above, the socket fails to recv() but then again it start to work if you have:

WSADATA wsaData;
WSAStartup(0x202, &wsaData);

Based on documentation, it says "It allows an application or DLL to specify the version of Windows Sockets required and retrieve details of the specific Windows Sockets implementation". So, could we not directly define a memory location to store the returned value?

question from:https://stackoverflow.com/questions/65910249/initialize-winsock-without-wsadata-using-wsastartup

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Basically I wanted to know if you can initialize a winsock without creating WSADATA variable or feeding the WSADATA value directly to WSAStartup function?

The call to WSAStartup() is required, otherwise most Winsock functions will fail with an WSANOTINITIALISED error. And WSAStartup() itself requires a pointer to allocated memory to fill in with WSADATA data. This is not optional.

So, could we not directly define a memory location to store the returned value?

That is exactly what declaring a variable does. But more generally, yes you can certainly pass in any non-NULL memory address, as long as it is a valid memory address within the calling process, and has been allocated to hold at least sizeof(WSADATA) number of bytes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...