原始套接字绑定出错,看不出原因,求解,贴出代码
程序代码:
void CCaptrueDlg::OnButton1() { // TODO: Add your control notification handler code here WSADATA wsadata; if(0!=WSAStartup(MAKEWORD(2,2),&wsadata)) { CString str; str.Format("WSAStartUp初始化失败,错误代码:%d",GetLastError()); MessageBox(str); return; } SOCKET Socket; Socket=socket(AF_INET,SOCK_RAW,IPPROTO_IP); if(INVALID_SOCKET==Socket) { CString str; str.Format("Socket创建失败,错误代码:%d",GetLastError()); MessageBox(str); WSACleanup(); return; } int RecvTime=5000; if(setsockopt(Socket,SOL_SOCKET,SO_RCVTIMEO,(CHAR *)&RecvTime, sizeof(RecvTime))!=0) { CString str; str.Format("设置Socket选项失败,错误代码:%d",GetLastError()); MessageBox(str); closesocket(Socket); WSACleanup(); return; } char HostName[128]={0}; if(gethostname(HostName,128)!=0) { CString str; str.Format("获得主机名失败,错误代码:%d",GetLastError()); MessageBox(str); closesocket(Socket); WSACleanup(); return; } hostent *phost=NULL; phost=gethostbyname(HostName); if(phost==NULL) { CString str; str.Format("获取主机地址失败,错误代码:%d",GetLastError()); MessageBox(str); closesocket(Socket); WSACleanup(); return; } sockaddr_in HostAddr; memset(&HostAddr,0,sizeof(HostAddr)); HostAddr.sin_family=AF_INET; HostAddr.sin_port=htons(7000); HostAddr.sin_addr.S_un.S_addr=(*(in_addr *)phost->h_addr_list).S_un.S_addr; if(bind(Socket,(sockaddr*)&HostAddr,sizeof(HostName))!=0) { CString str; str.Format("绑定Socket失败,错误代码:%d",GetLastError()); MessageBox(str); closesocket(Socket); WSACleanup(); return; } UpdateData();