VC:检查端口是否打开

时间:2020-12-15 15:21:22

1、

void MySocket::OnConnect(int nErrorCode)

{

       // TODO: Add your specialized code here and/or call the base class

       if(nErrorCode!=0)                              //连接失败

              AfxMessageBox("端口关闭");

       else

              AfxMessageBox("端口打开");

 

       CAsyncSocket::OnConnect(nErrorCode);

}

2、

       CString m_ipstr;

       MySocket m_socket;

 

void CTestDlg::OnConnect()

{

       // TODO: Add your control notification handler code here

       if (!AfxSocketInit())

       {

              AfxMessageBox("IDP_SOCKETS_INIT_FAILED");

              return ;

       }

       GetDlgItemText(IDC_IPADDRESS1,m_ipstr);

       m_socket.m_hSocket=INVALID_SOCKET;

       UpdateData(true);

//创建套接字

       BOOL flag=m_socket.Create();

       if(!flag)

       {

              AfxMessageBox("SOCKET ERROR");

              return;

       }

       m_socket.Connect(m_ipstr,m_port);//同时调用新建的类中的函数检测端口

 

}