现在的位置: 首页 > 综合 > 正文

VC:检查端口是否打开

2012年12月24日 ⁄ 综合 ⁄ 共 823字 ⁄ 字号 评论关闭

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);//同时调用新建的类中的函数检测端口

 

}

抱歉!评论已关闭.