原来是在对话框下建立的聊天程序,没一点问题。现在我想把它移到单文档下建立,并且分割视图,新建了一个基于CFormView的CTextForm类,在其对话框中放入了一些控件。
然后利用下面的代码实现网络连接:
void CTextForm::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
m_strNickname="sdfa";
int nReturnValue; m_ConnectToServerSocket.Create();
nReturnValue = m_ConnectToServerSocket.Connect("127.0.0.1", 5216);
if(nReturnValue)
{
m_DataSocket.Create(0, SOCK_DGRAM);
CString s;
UINT port;
m_DataSocket.GetSockName(s, port);
s.Format("%d", port);
s += "/" + m_strNickname;
m_ConnectToServerSocket.Send(s, s.GetLength());
SetDlgItemText(IDC_MESSAGEDISP, "成功登陆到服务器!");
CListBox *pListBox = (CListBox *)GetDlgItem(IDC_MEMBERLIST);
pListBox->AddString("所有人");
pListBox->SetCurSel(0);
}
else
{
MessageBox("连接服务器失败!");
}
}
运行程序,连接成功,接着提示终止程序。为什么?请高手指点。谢谢!!
然后利用下面的代码实现网络连接:
void CTextForm::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
m_strNickname="sdfa";
int nReturnValue; m_ConnectToServerSocket.Create();
nReturnValue = m_ConnectToServerSocket.Connect("127.0.0.1", 5216);
if(nReturnValue)
{
m_DataSocket.Create(0, SOCK_DGRAM);
CString s;
UINT port;
m_DataSocket.GetSockName(s, port);
s.Format("%d", port);
s += "/" + m_strNickname;
m_ConnectToServerSocket.Send(s, s.GetLength());
SetDlgItemText(IDC_MESSAGEDISP, "成功登陆到服务器!");
CListBox *pListBox = (CListBox *)GetDlgItem(IDC_MEMBERLIST);
pListBox->AddString("所有人");
pListBox->SetCurSel(0);
}
else
{
MessageBox("连接服务器失败!");
}
}
运行程序,连接成功,接着提示终止程序。为什么?请高手指点。谢谢!!
这个看不出来为什么你的程序退出了
怀疑你的代码有问题,
例如:CListBox *pListBox = (CListBox *)GetDlgItem(IDC_MEMBERLIST);
你要检查pListBox 是否 == NULL.
m_ConnectToServerSocket.Send(s.GetBuffer(0), s.GetLength());
void CCTSSocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CTextForm *pClientDlg = (CTextForm *)AfxGetMainWnd();
CString strMessage, strRichEdit;
CString strAddress, strNickName, strPort;
UINT nPort;
int i,j;
char szMessage[128];
CUser *pUser = NULL;
memset(szMessage, 0, 128);
Receive(szMessage, 128);
// pClientDlg->MessageBox(szMessage);
strMessage.Format("%s", szMessage);
i = strMessage.Find('/');
strAddress = strMessage.Mid(3, i-3);
j = strMessage.Find('/', i+1);
strPort = strMessage.Mid(i+1, j-i-1);
nPort = atoi(strPort);
strNickName = strMessage.Mid(j+1, strMessage.GetLength()-j-1);
CListBox *pListBox = (CListBox *)pClientDlg->GetDlgItem(IDC_MEMBERLIST);
pClientDlg->GetDlgItemText(IDC_MESSAGEDISP, strRichEdit);
if(strMessage.Mid(0,3).Compare("Add") == 0)
{
// pClientDlg->MessageBox(strNickName);
if(pListBox->FindString(0, strNickName) == LB_ERR)
{
pUser = new CUser(strAddress, nPort, strNickName);
pClientDlg->m_UserList.AddTail(pUser);
pListBox->AddString(strNickName);
pClientDlg->SetDlgItemText(IDC_MESSAGEDISP, strRichEdit);
}
}
else
if(strMessage.Mid(0,3).Compare("Del") == 0)
{
if(strNickName == pClientDlg->m_strNickname) //自己被踢出
{
strRichEdit += "\r\n***** 您被踢出了聊天室 *****";
pClientDlg->SetDlgItemText(IDC_MESSAGEDISP, strRichEdit);
CRichEditCtrl *pRich = ((CRichEditCtrl *)pClientDlg->GetDlgItem(IDC_MESSAGEDISP));
if(pRich->GetLineCount() > 14)
{
pRich->LineScroll(1, 0);
pRich->LineScroll(1, 0);
}
pListBox->ResetContent();
pClientDlg->SetDlgItemText(IDC_MEMBERSELECT, "");
pClientDlg->SetDlgItemText(IDC_MESSAGEINPUT, "");
pClientDlg->SetDlgItemText(IDC_EDIT_Memcount, "");
}
else //其他人正常退出
{
for(i=0; i<pClientDlg->m_UserList.GetCount(); i++)
{
pUser = pClientDlg->m_UserList.GetAt(pClientDlg->m_UserList.FindIndex(i));
if(pUser->m_strNickName = strNickName)
{
pClientDlg->m_UserList.RemoveAt(pClientDlg->m_UserList.FindIndex(i));
pListBox->DeleteString(pListBox->FindString(0, strNickName));
strRichEdit += "\r\n***** " + strNickName + " 离开了聊天室 *****";
pClientDlg->SetDlgItemText(IDC_MESSAGEDISP, strRichEdit);
break;
}
CRichEditCtrl *pRich = ((CRichEditCtrl *)pClientDlg->GetDlgItem(IDC_MESSAGEDISP));
if(pRich->GetLineCount() > 14)
{
pRich->LineScroll(1, 0);
pRich->LineScroll(1, 0);
}
}
}
}
long MCount=pClientDlg->m_UserList.GetCount();
CString temp;temp.Format("%d",MCount);
pClientDlg->SetDlgItemText(IDC_EDIT_Memcount,temp); CSocket::OnReceive(nErrorCode);
}
{
pUser = new CUser(strAddress, nPort, strNickName);
pClientDlg->m_UserList.AddTail(pUser);
pListBox->AddString(strNickName);
pClientDlg->SetDlgItemText(IDC_MESSAGEDISP, strRichEdit);
}
里面的if(pListBox->FindString(0, strNickName) == LB_ERR)这一句有问题