将IP_HDRINCL设置为win32中的setsockopt函数
我正在与 Win32 中的原始套接字进行斗争,现在我陷入了困境,setsockopt() 函数给了我 10022 错误(无效参数),但我认为我传递了正确的参数...当然我错了 u_u'
sock = socket(AF_INET,SOCK_RAW,IPPROTO_UDP);
if (sock == SOCKET_ERROR)
{
printf("Error socket(): %d", WSAGetLastError());
return;
}
char on = 1;
error = setsockopt(sock,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on));
if (sock == SOCKET_ERROR)
{
printf("Error setsockopt(): %d", WSAGetLastError());
return;
}
有人知道我的代码发生了什么吗?
I'm fighting with raw sockets in Win32 and now I'm stuck, the setsockopt()
function give me the 10022 error (invalid argument), but I think I pass the correct arguments... of course I'm wrong u_u'
sock = socket(AF_INET,SOCK_RAW,IPPROTO_UDP);
if (sock == SOCKET_ERROR)
{
printf("Error socket(): %d", WSAGetLastError());
return;
}
char on = 1;
error = setsockopt(sock,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on));
if (sock == SOCKET_ERROR)
{
printf("Error setsockopt(): %d", WSAGetLastError());
return;
}
Anybody knows what happen to my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 DWORD 1 或 bool true,有很多文档显示带有 char、int、DWORD、bool 的代码,但正确的一个是 book 或 DWORD,在某些系统中可能是 int...但目前它对我有用布尔。
顺便说一下,我在Win7中使用IP_HDRINCL执行相同的函数setsockopt时发现错误10014 WSAEFAULT,它在XP中工作,但在Win7中总是失败,不知道为什么......也许它在最近的Windows版本中被禁用?
You should use DWORD 1 or bool true, there are lots of documents that show code with char, int, DWORD, bool, but the right one is book or DWORD, maybe int in some systems... but currently it worked for me as bool.
By the way, I've found error 10014 WSAEFAULT when executing the same function setsockopt with IP_HDRINCL in Win7, it works in XP but in Win7 it always fails and don't know why... maybe it is disabled in recent windows versions?
据我记得你需要使用
int on = 1
而不是 char...As far as I remember you need to use
int on = 1
instead of char...