MFC SOCKET服务器端
想做一个简单的MFC聊天程序
发送端经测试可以使用但是就是服务器端一直连接不上
首先我自己定制一个类
class ClistenSocket : public CAsyncSocket
用这个类的对象监听端口
BOOL CReceiveDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
m_ListenSocket.Create(9000);//监听端口
if(m_ListenSocket.Listen(1)==FALSE)
{
AfxMessageBox("Unable to Listen on that port,please try another port");
m_ListenSocket.Close();
return FALSE;
}
void ReceiveSocket::OnAccept(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
Accept(((CReceiveDlg *)(AfxGetApp()->m_pMainWnd))->m_Socket);
//启用发送按钮
((CReceiveDlg *)(AfxGetApp()->m_pMainWnd))->m_ButtonSend.EnableWindow();
((CReceiveDlg *)(AfxGetApp()->m_pMainWnd))->m_Socket.AsyncSelect(
FD_READ|FD_WRITE|FD_CLOSE);
((CReceiveDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.AddString(
"接受客户端的请求");
CAsyncSocket::OnAccept(nErrorCode);
}