setsockopt错误

sidooh 2009-09-28 01:23:31
Windows XP下用setsockopt时发生错误


int optval = 1;
SOCKET ConnectSocket=INVALID_SOCKET;
ConnectSocket=socket(AF_INET,SOCK_RAW,IPPROTO_RAW);
setsockopt(ConnectSocket,IPPROTO_IP,IP_HDRINCL,(char *)&optval,sizeof(optval));


错误描述:Program received signal SIGSEGV,Segmentation fault.



这个该怎么解决啊,谢谢大家了!
...全文
267 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhu6789888 2009-09-29
  • 打赏
  • 举报
回复
绑定帮定
sidooh 2009-09-29
  • 打赏
  • 举报
回复
我调试的时候 程序运行到
setsockopt(ConnectSocket,IPPROTO_IP,IP_HDRINCL,(char *)&optval,sizeof(optval));

ConnectSocket的值就变成invalid symbol了

运行到这里就不能运行下去了
sidooh 2009-09-28
  • 打赏
  • 举报
回复

#define WINVER 0x0501

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>



int main(){
WSADATA wsaData;
int iResult;
char send_packet[15] = {0x08,0xae,0x00,0x0b,0x61,0x72,0x72,0x6f,0x77,0x5f,0x72,0x69,0x67,0x68,0x74};


// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}

struct addrinfo *result=NULL,*ptr=NULL,hints;


ZeroMemory(&hints,sizeof(hints));//????????????????
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
hints.ai_protocol=IPPROTO_TCP;
iResult=getaddrinfo("207.229.97.12","2222",&hints,&result);
printf("%d\n",iResult);

ptr=result;
int optval = 1;

SOCKET ConnectSocket=INVALID_SOCKET;
// ConnectSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
ConnectSocket=socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
setsockopt(ConnectSocket,IPPROTO_IP,IP_HDRINCL,(char *)&optval,sizeof(optval));
if(ConnectSocket==INVALID_SOCKET) printf("Faliure\n");

// iResult=connect(ConnectSocket,ptr->ai_addr,ptr->ai_addrlen);
// iResult = send(ConnectSocket,send_packet,15,0);
// if(iResult==SOCKET_ERROR) printf("Failure\n");
return 0;
}
sidooh 2009-09-28
  • 打赏
  • 举报
回复
这是我的代码,好像没有用到多线程,麻烦看一下,谢啦!



#define WINVER 0x0501

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>



int main(){
WSADATA wsaData;
int iResult;
char send_packet[15] = {0x08,0xae,0x00,0x0b,0x61,0x72,0x72,0x6f,0x77,0x5f,0x72,0x69,0x67,0x68,0x74};


// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}

struct addrinfo *result=NULL,*ptr=NULL,hints;


ZeroMemory(&hints,sizeof(hints));//????????????????
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
hints.ai_protocol=IPPROTO_TCP;
iResult=getaddrinfo("207.229.97.12","2222",&hints,&result);
printf("%d\n",iResult);

ptr=result;
int optval = 1;

SOCKET ConnectSocket=INVALID_SOCKET;
// ConnectSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
ConnectSocket=socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
setsockopt(ConnectSocket,IPPROTO_IP,IP_HDRINCL,(char *)&optval,sizeof(optval));
if(ConnectSocket==INVALID_SOCKET) printf("Faliure\n");

// iResult=connect(ConnectSocket,ptr->ai_addr,ptr->ai_addrlen);
// iResult = send(ConnectSocket,send_packet,15,0);
// if(iResult==SOCKET_ERROR) printf("Failure\n");
return 0;
}
oyster2008 2009-09-28
  • 打赏
  • 举报
回复
段错误了,是内存非法访问
PiggyXP 2009-09-28
  • 打赏
  • 举报
回复
楼主的错误应该不是出在这里吧,你是否其他可能会出现问题的代码呢?比如使用了多线程什么的?

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧