📄 testnetdlg.cpp
字号:
/*
* Openmysee
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
//
#include "stdafx.h"
#include "GVCapture.h"
#include "TestNetDlg.h"
#include ".\testnetdlg.h"
const int SOCKET_MAJOR_VERSION = 2;
const int SOCKET_MINOR_VERSION = 2;
const int SOCKET_SUCCESS = 0;
// CTestNetDlg dialog
IMPLEMENT_DYNAMIC(CTestNetDlg, CDialog)
CTestNetDlg::CTestNetDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestNetDlg::IDD, pParent)
{
InitializeCriticalSection(&moCriticalSection);
}
CTestNetDlg::~CTestNetDlg()
{
UnInitSock();
DeleteCriticalSection(&moCriticalSection);
}
void CTestNetDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMBO_IP_NET, mcomboxIPAry);
DDX_Text(pDX, IDC_STATE_EDIT, mstrState);
}
void CTestNetDlg::GetAddr(CString astrSPAddress)
{
CString lstrSpAddress;
while (-1 != astrSPAddress.Find(':'))
{
lstrSpAddress = astrSPAddress.Left(astrSPAddress.Find(':'));
lstrSpAddress.Trim();
mStrAryIPAry.Add(lstrSpAddress);
astrSPAddress = astrSPAddress.Right(astrSPAddress.GetLength() \
- astrSPAddress.Find(':') - 1);
}
if (0 != astrSPAddress.GetLength())
{
lstrSpAddress = astrSPAddress;
lstrSpAddress.Trim();
mStrAryIPAry.Add(lstrSpAddress);
}
}
BEGIN_MESSAGE_MAP(CTestNetDlg, CDialog)
ON_BN_CLICKED(IDC_TEST, OnBnClickedTest)
ON_BN_CLICKED(IDC_BTN_TEST_ALL, OnBnClickedBtnTestAll)
END_MESSAGE_MAP()
// CTestNetDlg message handlers
BOOL CTestNetDlg::OnInitDialog()
{
CDialog::OnInitDialog();
if (SOCKET_SUCCESS != InitSock())
{
AfxMessageBox("初始化WinSock失败,测试失败,请退出");
WSACleanup();
return TRUE;
}
for(int i = 0; i < mStrAryIPAry.GetSize(); i++)
{
mcomboxIPAry.AddString(mStrAryIPAry.GetAt(i));
}
mcomboxIPAry.SetCurSel(0);
// TODO: Add extra initialization here
mstrState = "请点击测试按钮开始测试";
//GetDlgItem(IDC_BTN_TEST_ALL)->SetFocus();
::SetFocus(GetDlgItem(IDC_BTN_TEST_ALL)->GetSafeHwnd());
UpdateData(false);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CTestNetDlg::OnBnClickedTest()
{
int i = 0;
CString lstrCurSel;
CONNECT_RESULT ret = CR_ERROR;
GetDlgItem(IDC_TEST)->SetWindowText("正在测试");
GetDlgItem(IDC_TEST)->EnableWindow(false);
if((i = mcomboxIPAry.GetCurSel()) != CB_ERR)
{
mcomboxIPAry.GetLBText(i, lstrCurSel);
}
//lsockAddr = GetCurAddrin(lstrCurSel);
mstrState = "正在测试lstrCurSel,请稍后";
GetDlgItem(IDC_STATE_EDIT)->SetWindowText( "正在测试lstrCurSel,请稍后");
//UpdateData(false);
if (TRUE == TestNet(lstrCurSel))
{
mstrState = lstrCurSel + "连通";
}
else
{
mstrState =lstrCurSel + "未连接";
}
UpdateData(false);
GetDlgItem(IDC_TEST)->EnableWindow();
GetDlgItem(IDC_TEST)->SetWindowText("测试");
}
//与网络相关的函数
SOCKADDR_IN CTestNetDlg::GetCurAddrin(CString astrIPAddr)
{
SOCKADDR_IN lsockAddr;
ZeroMemory(&lsockAddr,sizeof(lsockAddr));
lsockAddr.sin_family = AF_INET;
lsockAddr.sin_port = htons(20);
lsockAddr.sin_addr.s_addr = inet_addr((LPCTSTR)astrIPAddr);
return lsockAddr;
}
//初始化WinSock
int CTestNetDlg::InitSock()
{
WSADATA WSD;
WORD wVersionRequired = MAKEWORD(SOCKET_MAJOR_VERSION, SOCKET_MINOR_VERSION);
ZeroMemory(&WSD,sizeof(WSADATA));
int nErrorNo = WSAStartup(wVersionRequired, &WSD);
if ( SOCKET_SUCCESS != nErrorNo )
{
return ( SOCKET_ERROR );
}
if ( LOBYTE( WSD.wVersion ) != SOCKET_MINOR_VERSION ||
HIBYTE( WSD.wVersion ) != SOCKET_MAJOR_VERSION )
{
WSACleanup();
return (SOCKET_ERROR);
}
//成功初始化
return (SOCKET_SUCCESS);
}
//反初始化Winsock
void CTestNetDlg::UnInitSock()
{
WSACleanup();
}
//开始连接
CONNECT_RESULT CTestNetDlg::Connecting(SOCKET& asocket, CString astrIPAddr)
{
CONNECT_RESULT ret = CR_ERROR;
// Create a TCP/IP socket that is bound to the server.
// Microsoft Knowledge Base: WSA_FLAG_OVERLAPPED Is Needed for Non-Blocking Sockets
// http://support.microsoft.com/default.aspx?scid=kb;EN-US;179942
asocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
if(asocket == INVALID_SOCKET)
{
return ret;
}
// 不使用Nagle算法
BOOL bNoDelay = TRUE;
if(setsockopt(asocket, SOL_SOCKET, TCP_NODELAY, (const char*)&bNoDelay, sizeof(bNoDelay)) == SOCKET_ERROR)
{
return ret;
}
// Set this socket as a Non-blocking socket.
ULONG flag = 1;
if(ioctlsocket(asocket, FIONBIO, &flag) == SOCKET_ERROR)
{
return ret;
}
SOCKADDR_IN lsockAddr;
lsockAddr = GetCurAddrin(astrIPAddr);
// Connect to remote address
if(WSAConnect(asocket, (sockaddr*)&lsockAddr, sizeof(lsockAddr), NULL, NULL, NULL, NULL) == SOCKET_ERROR)
{
if(WSAGetLastError() != WSAEWOULDBLOCK)
{
return ret;
}
else
{
ret = CR_WOULDBLOCK;
}
}
else
{
ret = CR_CONNECTED;
}
return ret;
}
//断开连接
void CTestNetDlg::Disconnect(SOCKET& asocket)
{
if(asocket != INVALID_SOCKET)
{
closesocket(asocket);
asocket = INVALID_SOCKET;
}
}
//测试网络
BOOL CTestNetDlg::TestNet(CString astrCurIP)
{
BOOL lisConnected = FALSE;
fd_set write_set;
CONNECT_RESULT lnetret = CR_ERROR;
timeval timeout;
ZeroMemory(&timeout,sizeof(timeval));
SOCKET lsocket;
lnetret =Connecting(lsocket, astrCurIP);
if(lnetret == CR_WOULDBLOCK)
{
// 等待8秒钟,看能否连接上
FD_ZERO(&write_set);
FD_SET(lsocket, &write_set);
timeout.tv_sec = 8;
timeout.tv_usec = 0;
int s = select(0, NULL, &write_set, NULL, &timeout);
if(s > 0)
{
lisConnected = TRUE;
Disconnect(lsocket);
}
}
return lisConnected;
}
void CTestNetDlg::OnBnClickedBtnTestAll()
{
CString lstrIP;
CONNECT_RESULT lnetret = CR_ERROR;
UpdateData();
GetDlgItem(IDC_BTN_TEST_ALL)->SetWindowText("正在测试");
GetDlgItem(IDC_BTN_TEST_ALL)->EnableWindow(false);
if (0 == mStrAryIPAry.GetSize())
{
return ;
}
mpstruConnect = new STRU_CONNECT[mStrAryIPAry.GetSize()];
if (NULL == mpstruConnect)
{
return;
}
DWORD ldwThreadId = 0;
HANDLE lhHandle = NULL;
minum = 0;
for(int i = 0; i < mStrAryIPAry.GetSize(); i++)
{
mpstruConnect[i].mbResult = FALSE;
mpstruConnect[i].msocket = 0;
mpstruConnect[i].mstrIP = mStrAryIPAry.GetAt(i);
lhHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(CTestNetDlg::ConnectProc), this, 0, &ldwThreadId);
if (NULL == lhHandle)
{
AfxMessageBox("检测失败");
if (NULL != mpstruConnect)
{
for (i = 0; i < mStrAryIPAry.GetSize(); i++)
{
Disconnect(mpstruConnect[i].msocket);
}
delete []mpstruConnect;
mpstruConnect = NULL;
}
return;
}
CloseHandle(lhHandle);
Sleep(200);
}
Sleep(1000);
GetResult();
DisplayResult();
GetDlgItem(IDC_BTN_TEST_ALL)->SetWindowText("测试全部");
GetDlgItem(IDC_BTN_TEST_ALL)->EnableWindow();
if (NULL != mpstruConnect)
{
for (i = 0; i < mStrAryIPAry.GetSize(); i++)
{
Disconnect(mpstruConnect[i].msocket);
}
delete []mpstruConnect;
mpstruConnect = NULL;
}
}
void WINAPI CTestNetDlg::ConnectProc(CTestNetDlg* apTestNetDlg)
{
int i = 0; //取得该线程的序号
CONNECT_RESULT lnetret = CR_ERROR; //运行结果
fd_set lwrite_set; //Select管理的套接字
timeval timeout;
ZeroMemory(&timeout,sizeof(timeval));
if (NULL == apTestNetDlg->mpstruConnect)
{
return ;
}
EnterCriticalSection(&apTestNetDlg->moCriticalSection);
i = apTestNetDlg->minum;
++ apTestNetDlg->minum;
LeaveCriticalSection(&apTestNetDlg->moCriticalSection);
//进行连接
lnetret = apTestNetDlg->Connecting(apTestNetDlg->mpstruConnect[i].msocket, apTestNetDlg->mpstruConnect[i].mstrIP);
if(lnetret == CR_WOULDBLOCK)
{
// 等待8秒钟,看能否连接上
FD_ZERO(&lwrite_set);
FD_SET(apTestNetDlg->mpstruConnect[i].msocket, &lwrite_set);
timeout.tv_sec = 8;
timeout.tv_usec = 0;
int s = select(0, NULL, &lwrite_set, NULL, &timeout);
if(s > 0)
{
apTestNetDlg->mpstruConnect[i].mbResult = TRUE;
apTestNetDlg->Disconnect(apTestNetDlg->mpstruConnect[i].msocket);
}
}
}
void CTestNetDlg::GetResult()
{
bool lbRun;
DWORD ldwElaps = 0;
ldwElaps = GetTickCount();
do
{
lbRun = false;
for (int i = 0; i < mStrAryIPAry.GetSize(); i++)
{
if (INVALID_SOCKET != mpstruConnect[i].msocket)
{
lbRun = true;
break;
}
}
if (ldwElaps + 10000 < GetTickCount())
{
lbRun = false;
}
}
while (lbRun);
}
void CTestNetDlg::DisplayResult()
{
mstrState = "全部网络测试结果:";
for (int i = 0; i < mStrAryIPAry.GetSize(); i++)
{
mstrState += mpstruConnect[i].mstrIP;
if (TRUE == mpstruConnect[i].mbResult)
{
mstrState += "连接成功";
}
else
{
mstrState += "连接失败";
}
}
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -