MFC-TCP连接代码片段(支援大富的)

 


BOOL CClientSocketTestDlg::OnInitDialog() { CDialogEx::OnInitDialog(); ........................
// TODO: 在此添加额外的初始化代码 AfxSocketInit();//Socket初始化 ........................ return TRUE; // 除非将焦点设置到控件,否则返回 TRUE }

#include <afxsock.h>

CSocket m_ClientSocket;

void CClientSocketTestDlg::OnBnClickedButtonConnect()
{
    // TODO:  在此添加控件通知处理程序代码
    UpdateData(true);

    if (!m_ClientSocket.Create())
    {
        MessageBox(L"Socket Create Failed!");
    }
    if (m_ClientSocket.Connect(serverIP, port))
    {
        //MessageBox(L"Connect the Server Successful!");
    }
    else
    {
        int nErr = GetLastError();
        MessageBox(L"Connect the Server Failed!");
    }

    SetTimer(1, deltaTime, NULL);
}
void CClientSocketTestDlg::OnTimer(UINT_PTR nIDEvent)
{
    // TODO:  在此添加消息处理程序代码和/或调用默认值
    switch (nIDEvent)
    {
    case 1:
    {
              if (!m_ClientSocket.Create())
              {
                  MessageBox(L"Socket Create Failed!");
              }
              if (m_ClientSocket.Connect(serverIP, port))
              {
                  allConnNum++;
                  //MessageBox(L"Connect the Server Successful!");
              }
              else
              {
                  int nErr = GetLastError();
                  MessageBox(L"Connect the Server Failed!");
              }
                  break;
    };
    case 2:
    {
              
    }
    default:    break;
    }
    UpdateData(FALSE);
    CDialogEx::OnTimer(nIDEvent);
}


void CClientSocketTestDlg::OnBnClickedButtonDisconnect()
{
    // TODO:  在此添加控件通知处理程序代码
    KillTimer(1);
}

 

posted @ 2017-07-10 11:07  酷熊  阅读(948)  评论(0编辑  收藏  举报