本文主要是介绍MFC基于TCP协议的CSocket类套接字服务器端代码示范,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
MFC基于TCP协议的CSocket类套接字服务器端代码示范
源文件下载:
文件名:
MFC_TCP_CSocket_Server.rar
关键代码:
void CMFC_TCP_CSocket_ServerDlg::AcceptConnect()
{CClientSocket* psocket = new CClientSocket();psocket->SetDialog(this);if (m_ServerSock.Accept(*psocket))m_socketlist.AddTail(psocket);else{delete psocket;}
}void CMFC_TCP_CSocket_ServerDlg::ReceiveData(CSocket& socket)
{char bufferdata[BUFFERSIZE];int len = socket.Receive(bufferdata,BUFFERSIZE);if (len != -1){bufferdata[len] = 0;POSITION pos = m_socketlist.GetHeadPosition();while (pos != NULL){CClientSocket* socket = (CClientSocket*)m_socketlist.GetNext(pos);if (socket != NULL)socket->Send(bufferdata,len);}}}void CMFC_TCP_CSocket_ServerDlg::OnBnClickedButtonConfig()
{// TODO: 在此添加控件通知处理程序代码m_ServerSock.SetDialog(this);CString strPort, strIP;m_ServerPort.GetWindowText(strPort);m_ServerIP.GetWindowText(strIP);if (!strPort.IsEmpty() && !strIP.IsEmpty()){UINT port = atoi(strPort);m_ServerSock.Create(port,SOCK_STREAM,strIP);BOOL ret = m_ServerSock.Listen();if (ret)MessageBox("设置成功","提示");GetDlgItem(IDC_BUTTON_Config)->EnableWindow(false);}}
这篇关于MFC基于TCP协议的CSocket类套接字服务器端代码示范的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!