MFC中单文档中WSAAsyncSelect参数问题!懂者,望来请教(虽然分不多。。。。)
- C/C++ code
int CFiveQiView::Login(){ CEnter userenter; userenter.DoModal(); CView* pView=(CView*)((CFrameWnd*)AfxGetApp()-> m_pMainWnd)-> GetActiveView(); [color=#FF00FF]//这里出错(CFrameWnd*)AfxGetApp()-> m_pMainWnd是空的??如何纠正[/color] WSADATA wsd; if (WSAStartup(MAKEWORD(2,2),&wsd)!=0) { AfxMessageBox(_T("动态库套接字初始化失败")); return 0; } if ((m_socket=socket(AF_INET,SOCK_DGRAM,0))==INVALID_SOCKET) { AfxMessageBox(_T("套接字创建失败!")); WSACleanup(); return 0; } //--------------------------获取本机(CString类型)IP----------------------------------- PHOSTENT hostinfo; char name[200]; CString IP; if (gethostname(name,sizeof(name))==0)//获取主机名 { if ((hostinfo=gethostbyname(name))!=NULL)//通过主机名获取本地IP { IP=inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);//将获取的IP转换为字符串 } } //---------------------------绑定套接字----------------------------------------------------- SOCKADDR_IN serverAddr; char *serverIP = new char[20]; WideCharToMultiByte(CP_ACP,0,IP.GetBuffer(),-1,serverIP,20,NULL,NULL);//Unicode转换成多字节字符串 serverAddr.sin_family=AF_INET; serverAddr.sin_port=htons(5050); serverAddr.sin_addr.S_un.S_addr=inet_addr(serverIP);//此处的inet_addr()里的参数需要的是多字节里的CString类型或Unicode里的char *类型,故上面用到WideCharToMultiByte if(bind(m_socket,(SOCKADDR *)&serverAddr,sizeof(serverAddr))==SOCKET_ERROR) { AfxMessageBox(_T("绑定IP失败!")); closesocket(m_socket); WSACleanup(); } if (WSAAsyncSelect(m_socket,pView->GetSafeHwnd(),UM_SOCK,FD_READ)==SOCKET_ERROR[color=#FF00FF])//这里第二个参数该填什么[/color] { AfxMessageBox(_T("失败!")); closesocket(m_socket); WSACleanup(); return 0; }}[解决办法]