怎样利用及区分多个连接socket进行接收数据,急急急
garyu 2007-04-23 08:48:31 怎样利用多个连接socket进行接收数据,以下代码是服务器端(CSocket类)
void CListenSocket::OnAccept(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CAcceptSocket *pSocket=new CAcceptSocket;
CDspDlg* pDlg=(CDspDlg*)AfxGetMainWnd(); //得到主框口(对话框)指针
pDlg->m_bLinked=TRUE;
if(Accept(*pSocket))
{
pDlg->m_pAcceptList.AddTail(pSocket);
CString strAddr,strAddr1;
UINT unPort,unPort1;
pSocket->GetPeerName(strAddr1,unPort1); //得到远程IP地址和端口号
pSocket->GetSockName(strAddr,unPort); //得到本地IP地址和端口号
pDlg->m_strNetMsg.Format("本地IP%s端口%d连接上远程客户IP%s端口%d",
strAddr,unPort,strAddr1,unPort1);
pDlg->UpdateMsgData();
}
else
{
delete pSocket;
}
CSocket::OnAccept(nErrorCode);
}
void CAcceptSocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
char chMsg[5120], chMsgTemp[1024];
UINT unRXCharNum; //每次读取的字符数
BOOL bEndFlag=0; //接收完毕标志
strcpy(chMsg,"");
do
{
strcpy(chMsgTemp,"");
unRXCharNum=Receive(chMsgTemp,1000);
if(unRXCharNum>1000||unRXCharNum<=0)
{
AfxMessageBox("接收数据中出错",MB_OK);
return;
}
else if(unRXCharNum<1000 && unRXCharNum>0)
{
bEndFlag=1;
}
chMsgTemp[unRXCharNum]=0; //加上字符串结束位
strcat(chMsg,chMsgTemp);
}while(bEndFlag==0);
CDspDlg* pDlg=(CDspDlg*)AfxGetMainWnd(); //得到主框口(对话框)指针
pDlg->m_strNetMsg.Format("接收到:%s",chMsg);
pDlg->UpdateMsgData(); //在主对话的接收编辑框中显示网络端口接收到的数据
CString strtemp;
strtemp.Format("%s",chMsg);
strtemp="服务器已收到" + strtemp; //组成回传信息
Send(strtemp,strtemp.GetLength(),0); //发送回传信息
CSocket::OnReceive(nErrorCode);
}
在CListenSocket::OnAccept(int nErrorCode )中CAcceptSocket *pSocket=new CAcceptSocket;并且pDlg->m_pAcceptList.AddTail(pSocket);怎么区分是哪个CListenSocket呢 可只要一来数据就调用void CAcceptSocket::OnReceive(int nErrorCode),,我想在客户端(在客户端我建立了多个socket)与服务器端建立多个连接,