您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> VC/MFC >> MFC中的两个Socket程序无法通信,请指教

MFC中的两个Socket程序无法通信,请指教

来源:网络整理     时间:2016/7/28 5:48:49     关键词:

关于网友提出的“ MFC中的两个Socket程序无法通信,请指教”问题疑问,本网通过在网上对“ MFC中的两个Socket程序无法通信,请指教”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: MFC中的两个Socket程序无法通信,请指教
描述:

这是客户端的主要程序程序:
void CNClientDlg::OnButtonStart() //连接服务器
{
// TODO: Add your control notification handler code here
CString strIP="";
CString temp="";
GetDlgItemText(IDC_EDIT_IP,strIP);
GetDlgItemText(IDC_EDIT_PORT,temp);
int nport=atoi(temp);
while(!pSocket->Connect(strIP,nport))
{
if(AfxMessageBox(IDS_CONNECTFAILED,MB_YESNO)==IDNO)
{
delete pSocket;
pSocket=NULL;
return;
}
}
}
void CNClientDlg::OnButtonSend() 
{
// TODO: Add your control notification handler code here
CString temp="";
GetDlgItemText(IDC_EDIT_CONTENT,temp);
memset(m_Buf,0,sizeof(char)*100);
strcpy(m_Buf,temp);
pSocket->Send(m_Buf,strlen(temp),0);
}
void CNClientDlg::OnButtonCancel() 
{
// TODO: Add your control notification handler code here
}
void CNClientDlg::SetData()
{
CString temp="";
GetDlgItemText(IDC_EDIT_SCONTENT,temp);
memset(m_Buf,0,sizeof(char)*100);
pSocket->Receive(m_Buf,100,0);
temp+=m_Buf;
SetDlgItemText(IDC_EDIT_SCONTENT,temp);
}
一下是服务器端的主要内容:
/*为该窗口创建一个侦听对象*/
void CNServerDlg::OnButtonStart() 
{
// TODO: Add your control notification handler code here
CString temp="";
GetDlgItemText(IDC_EDIT_PORT,temp);
int nport=atoi(temp);
m_pSocket=new CListening(this); //为该窗口创建一个侦听对象
if(m_pSocket->Create(nport))
{
if(m_pSocket->Listen())
{
return;
}
else 
AfxMessageBox("Cannot listen");
}
else
AfxMessageBox("Cannot creat the socket");
}
void CNServerDlg::OnButtonSend() 
{
// TODO: Add your control notification handler code here
CString temp="";
GetDlgItemText(IDC_EDIT_CONTENT,temp);
memset(m_Buf,0,sizeof(char)*100);
strcpy(m_Buf,temp);
m_pSocket->Send(m_Buf,strlen(temp),0);
}
void CNServerDlg::OnButtonCnacel() 
{
// TODO: Add your control notification handler code here
}
void CNServerDlg::AcceptConnect() //接受客户端的连接请求
{
pSocket=new Cclient(this);
if(!m_pSocket->Accept(*pSocket)) //接受连接请求
{
AfxMessageBox("cannot accept connect");
}
}
void CNServerDlg::ReadData()
{
CString temp="";
GetDlgItemText(IDC_EDIT_SCONTENT,temp);
memset(m_Buf,0,sizeof(char)*100);
pSocket->Receive(m_Buf,100,0);
temp+=m_Buf;
SetDlgItemText(IDC_EDIT_SCONTENT,temp);
}
和服务器端指定的端口有关吗?
我尝试了一些端口都不行,请问在程序怎样增加一些检错(获得程序错误代号)的函数?
请高手指教啊!!


解决方案1:

WSAGetLastError得到错误码


以上介绍了“ MFC中的两个Socket程序无法通信,请指教”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2980574.html

相关图片

相关文章