服务器代码:
BOOL CChatServerApp::InitInstance()
{
AfxEnableControlContainer();
SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings();  // Load standard INI file options (including MRU)
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CChatServerDoc),
RUNTIME_CLASS(CMainFrame),       // main SDI frame window
RUNTIME_CLASS(CChatServerView));
AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE; // The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
m_pMainWnd->SetWindowText("在线用户");
//
if (!AfxSocketInit())
{
return FALSE;
} CSetServerPortDlg Dlg;
if(Dlg.DoModal() == IDCANCEL)
{
return FALSE;
}
else
{
if(!m_skServerSocket.Create(Dlg.m_iServerPort))
{
return FALSE;
}
}
if(!m_skServerSocket.Listen(5))
{
return FALSE;
}
return TRUE;
}void CServerSocket::OnAccept(int nErrorCode) 
{
// TODO: Add your specialized code here and/or call the base class
CClientSocket * m_pNewClient = new CClientSocket();
theApp.m_pClientSocketList->AddTail(m_pNewClient);
CSocket::OnAccept(nErrorCode);
}void CClientSocket::OnReceive(int nErrorCode) 
{
         //.....处理了很多逻辑上的事务,考虑到代码量,我只有此简写
         AfxMessageBox("OnReceive");
         CSocket::OnReceive(nErrorCode);
}
/////////////////////客户端代码///////////////////////////
BOOL CChatClientApp::InitInstance()
{
AfxEnableControlContainer(); SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings();  // Load standard INI file options (including MRU) CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CChatClientDoc),
RUNTIME_CLASS(CMainFrame),       // main SDI frame window
RUNTIME_CLASS(CChatClientView));
AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE; // The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
m_pMainWnd->SetWindowText("聊天室");
//
memset(&msObj,0,sizeof(msObj));
if (!AfxSocketInit())
{
AfxMessageBox("Socket启动错误");
return FALSE;
}
if(!m_skMainSocket.Create())
{
AfxMessageBox("端口创建失败");
return FALSE;
}
CString strServerIp=_T("127.0.0.1");
int iServerPort=1080;
if(!m_skMainSocket.Connect(strServerIp,iServerPort))
{
AfxMessageBox("连接错误,请启动服务器");
return FALSE;
}
else//第一次发消息,通知服务器
{
CLogDlg  Dlg;
if(Dlg.DoModal()==IDCANCEL)
{
return FALSE;
}
CMessage msObj;
memset(&msObj,0,sizeof(msObj));
msObj.iType=0;
lstrcpy(msObj.strName,Dlg.m_strName);
m_skMainSocket.Send(&msObj,sizeof(CMessage));
}
return TRUE;
}/因为执行客户端的时候,启动时会Send一条记录,但是服务器端没有OnReceive到,因为OnReceive根本没有执行。