gpt4 book ai didi

C++ UDP 从 WSAGetLastError 10014 接收

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:20 25 4
gpt4 key购买 nike

这可能是一个重复的问题,但我已经阅读了其他线程和解决方案,并没有发现代码中遗漏任何内容。那里有些我无法弄清楚的东西。

下面是UDP服务器的代码

#pragma once
#pragma comment( linker, "/defaultlib:ws2_32.lib" )

#include <io.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <process.h>
#include <winsock.h>
#include <iostream>
#include <windows.h>
#include <string>
#include <stdio.h>
using namespace std;
#define REQUEST_PORT 0x7070
#define TIMEOUT_USEC 300000
#define MAX_RETRIES 3
int port=REQUEST_PORT;
//socket data types
SOCKET serverSocket;
SOCKET cs;
SOCKADDR_IN serverSocketAddr;
SOCKADDR_IN clientSocketAddr;
int senderAddrSize = sizeof (clientSocketAddr);
char *buffer;
char localhost[21];
HOSTENT *hp;

int main(void)
{
try
{
initializeSockets();
}
catch(char* str)
{
LPTSTR Error = 0;
if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,WSAGetLastError() | GetLastError(),0,(LPTSTR)&Error,0,NULL) == 0)
{
cout<<str<<endl;
}
else
{
cerr<<Error<<endl;
}
LocalFree(Error);
}
return 0;
}
void initializeSockets()
{
try
{
WSADATA wsadata;
if (WSAStartup(0x0202,&wsadata)!=0)
{
throw "Error in starting WSAStartup()";
}
else
{
buffer="WSAStartup was suuccessful\n";
}
gethostname(localhost,20);
cout<<"hostname: "<<localhost<< endl;
if((hp=gethostbyname(localhost)) == NULL)
{
cout << "Cannot get local host info."
<< WSAGetLastError() << endl;
exit(1);
}
if((serverSocket = socket(AF_INET,SOCK_DGRAM,0))==INVALID_SOCKET)
throw "can't initialize socket";
serverSocketAddr.sin_family = AF_INET;
serverSocketAddr.sin_port = htons(port);
serverSocketAddr.sin_addr.s_addr = htonl(INADDR_ANY);

if (::bind(serverSocket,(LPSOCKADDR)&serverSocketAddr,sizeof(serverSocketAddr)) == SOCKET_ERROR)
throw "can't bind the socket";
if(recvfrom(serverSocket,buffer,sizeof(buffer),0,(SOCKADDR *)&clientSocketAddr, &senderAddrSize)==SOCKET_ERROR)
throw "Error";

}
catch(char* str)
{
LPTSTR Error = 0;
if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,WSAGetLastError() | GetLastError(),0,(LPTSTR)&Error,0,NULL) == 0)
{
cout<<str<<endl;
}
else
{
cerr<<Error<<endl;
}
LocalFree(Error);
}
}

在 recvfrom 上,我收到 WSAError 10014:系统在尝试使用调用的指针参数时检测到无效指针地址。

我尝试将最后两个参数设置为 NULL,然后它工作正常,这意味着错误在这两个指针变量中。但是我已经正确地将 sockaddr_in 转换为 sockaddr 并且还使用 sizeof sockaddr 初始化了长度。仍然收到错误。不知道缺少什么。

最佳答案

documentation对于 recvfrom() 非常清楚导致 10014 (WSAEFAULT) 的原因:

WSAEFAULT
The buffer pointed to by the buf or from parameters are not in the user address space, or the fromlen parameter is too small to accommodate the source address of the peer address.

您正在将字符串文字分配给传递给 recvfrom()buffer:

buffer="WSAStartup was suuccessful\n";

字符串文字驻留在 recvfrom() 无法写入的只读内存中。

此外,buffer 声明为 char*,因此使用 sizeof(buffer) 是错误的。

需要为buffer分配可写内存,去掉无用的赋值,eg:

char buffer[65535];

那么 sizeof(buffer) 就有意义了。

关于C++ UDP 从 WSAGetLastError 10014 接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26418115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com