这个Socket令我很纠结、两边都接收不了,求大神看看
啥也不说了,贴代码:
=========================SocketServer===========================
-------------------------MySocket:-----------------------------
void CMySocket::OnAccept(int nErrorCode)
{
if(nErrorCode == 0)
m_pDlg->OnAccept();
}
void CMySocket::OnClose(int nErrorCode)
{
if(nErrorCode == 0)
m_pDlg->OnClose();
}
void CMySocket::OnConnect(int nErrorCode)
{
if(nErrorCode == 0)
m_pDlg->OnConnect();
}
void CMySocket::OnReceive(int nErrorCode)
{
if(nErrorCode == 0)
m_pDlg->OnReceive();
}
void CMySocket::SetParent(CSocketServerDlg *pDlg)
{
m_pDlg = pDlg;
}
--------------------SocketServerDlg------------------------
OnInitDialog():
m_strServName = "localhost";
m_nServPort = 1000;
m_sConnectSocket.SetParent(this);
m_sListenSocket.SetParent(this);
void CSocketServerDlg::OnButtonClose()
{
OnClose();
// TODO: Add your control notification handler code here
}
void CSocketServerDlg::OnButtonListen()
{
UpdateData(TRUE);
GetDlgItem(IDC_BUTTON_LISTEN)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_SERVNAME)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_SERVPORT)->EnableWindow(FALSE);
m_sListenSocket.Create(m_nServPort);
m_sListenSocket.Listen();
// TODO: Add your control notification handler code here
}
void CSocketServerDlg::OnSendMsg()
{
int nLen;
int nSent;
UpdateData(TRUE);
if(!m_strMsg.IsEmpty())
{
nLen = m_strMsg.GetLength();
nSent = m_sConnectSocket.Send(LPCTSTR(m_strMsg),255);
if(nSent != SOCKET_ERROR)
{
m_listSent.AddString(m_strMsg);
UpdateData(FALSE);
}
else
{
AfxMessageBox("ÐÅÏ¢·¢ËÍ´íÎó£¡",MB_OK | MB_ICONSTOP);
}
m_strMsg.Empty();
UpdateData(FALSE);
}
// TODO: Add your control notification handler code here
}
void CSocketServerDlg::OnClose()
{
m_listReceived.AddString("·þÎñÆ÷ÊÕµ½ÁËOnCloseÏûÏ¢£¡");
m_sConnectSocket.Close();
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(FALSE);
GetDlgItem(IDOK)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(FALSE);
while(m_listSent.GetCount() != 0)
m_listSent.DeleteString(0);
while(m_listReceived.GetCount() != 0)
m_listReceived.DeleteString(0);
}
void CSocketServerDlg::OnConnect()
{
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(TRUE);
GetDlgItem(IDOK)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(TRUE);
m_listReceived.AddString("·þÎñÆ÷ÊÕµ½ÁËOnConnectÏûÏ¢£¡");
}
void CSocketServerDlg::OnReceive()
{
m_listReceived.AddString("==½ÓÊÕ==");
char *pBuf = new char[1025];
int nBufSize = 1024;
int nReceived;
CString strReceived;
nReceived = m_sConnectSocket.Receive(pBuf,nBufSize);
if(nReceived != SOCKET_ERROR)
{
pBuf[nReceived] = NULL;
strReceived = pBuf;
m_listReceived.AddString(strReceived);
UpdateData(FALSE);
}
else
{
AfxMessageBox("ÐÅÏ¢½ÓÊÕ´íÎó£¡",MB_OK | MB_ICONSTOP);
}
}
void CSocketServerDlg::OnAccept()
{
m_listReceived.AddString("·þÎñÆ÷ÊÕµ½ÁËOnAcceptÏûÏ¢£¡");
m_sListenSocket.Listen(m_sConnectSocket);
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(TRUE);
GetDlgItem(IDOK)->EnableWindow(TRUE);
}
=========================SocketClient===========================
--------------------------MySocket----------------------------
void CMySocket::OnConnect(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if(nErrorCode == 0)
m_pDlg->OnConnect();
//CAsyncSocket::OnConnect(nErrorCode);
}
void CMySocket::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if(nErrorCode == 0)
m_pDlg->OnClose();
//CAsyncSocket::OnClose(nErrorCode);
}
void CMySocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if(nErrorCode == 0)
m_pDlg->OnReceive();
}
void CMySocket::SetParent(CSoketClientDlg *pDlg)
{
m_pDlg = pDlg;
}
-----------------------------------SocketClientDlg-----------------------
OnInitDialog():
m_strServName = "localhost";
m_nServPort = 1000;
m_strMsg = "";
m_sConnectSocket.SetParent(this);
void CSocketServerDlg::OnButtonClose()
{
OnClose();
// TODO: Add your control notification handler code here
}
void CSocketServerDlg::OnButtonListen()
{
UpdateData(TRUE);
GetDlgItem(IDC_BUTTON_LISTEN)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_SERVNAME)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_SERVPORT)->EnableWindow(FALSE);
m_sListenSocket.Create(m_nServPort);
m_sListenSocket.Listen();
// TODO: Add your control notification handler code here
}
void CSocketServerDlg::OnSendMsg()
{
int nLen;
int nSent;
UpdateData(TRUE);
if(!m_strMsg.IsEmpty())
{
nLen = m_strMsg.GetLength();
nSent = m_sConnectSocket.Send(LPCTSTR(m_strMsg),255);
if(nSent != SOCKET_ERROR)
{
m_listSent.AddString(m_strMsg);
UpdateData(FALSE);
}
else
{
AfxMessageBox("ÐÅÏ¢·¢ËÍ´íÎó£¡",MB_OK | MB_ICONSTOP);
}
m_strMsg.Empty();
UpdateData(FALSE);
}
// TODO: Add your control notification handler code here
}
void CSocketServerDlg::OnClose()
{
m_listReceived.AddString("·þÎñÆ÷ÊÕµ½ÁËOnCloseÏûÏ¢£¡");
m_sConnectSocket.Close();
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(FALSE);
GetDlgItem(IDOK)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(FALSE);
while(m_listSent.GetCount() != 0)
m_listSent.DeleteString(0);
while(m_listReceived.GetCount() != 0)
m_listReceived.DeleteString(0);
}
void CSocketServerDlg::OnConnect()
{
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(TRUE);
GetDlgItem(IDOK)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(TRUE);
m_listReceived.AddString("·þÎñÆ÷ÊÕµ½ÁËOnConnectÏûÏ¢£¡");
}
void CSocketServerDlg::OnReceive()
{
m_listReceived.AddString("==½ÓÊÕ==");
char *pBuf = new char[1025];
int nBufSize = 1024;
int nReceived;
CString strReceived;
nReceived = m_sConnectSocket.Receive(pBuf,nBufSize);
if(nReceived != SOCKET_ERROR)
{
pBuf[nReceived] = NULL;
strReceived = pBuf;
m_listReceived.AddString(strReceived);
UpdateData(FALSE);
}
else
{
AfxMessageBox("ÐÅÏ¢½ÓÊÕ´íÎó£¡",MB_OK | MB_ICONSTOP);
}
}
void CSocketServerDlg::OnAccept()
{
m_listReceived.AddString("·þÎñÆ÷ÊÕµ½ÁËOnAcceptÏûÏ¢£¡");
m_sListenSocket.Listen(m_sConnectSocket);
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(TRUE);
GetDlgItem(IDOK)->EnableWindow(TRUE);
}
源代码资源链接:http://download.csdn.net/source/3066636,连接没有问题,发送数据两边都无法接收,请大神相助。。。