📄 testdlg.cpp
字号:
MessageBox(strMsg,"错误");
closesocket(sock[n]);
return false;
}
WSAAsyncSelect(sock[n],GetSafeHwnd(),WM_SOCKET_MSG,FD_READ);
return true;
}
void CTestDlg::OnSocketMsg(WPARAM wParam, LPARAM lParam)
{
int message;
SOCKET curSock=(SOCKET)wParam;
message=lParam&0x0000FFFF;
char buf[1024],bufIP[20];
CString strMsg,str;
memset(buf,0,sizeof(buf));
memset(bufIP,0,sizeof(bufIP));
if(message==FD_READ)
{
sockaddr_in sender;
int len=sizeof(sender);
int ret=recvfrom(curSock,buf,1024,0,(LPSOCKADDR)&sender,&len);
if(ret==SOCKET_ERROR)
{
strMsg.Format ("读取数据失败,错误码:%d",WSAGetLastError());
MessageBox(strMsg,"错误");
return;
}
strcpy(bufIP,inet_ntoa(sender.sin_addr));
str.Format ("从%s接收到数据:%s\r\n",bufIP,buf);
if(curSock==sock[0])
m_sRecA+=str;
else if(curSock==sock[1])
m_sRecB+=str;
UpdateData(FALSE);
}
}
void CTestDlg::OnClose()
{
closesocket(sock[0]);
closesocket(sock[1]);
WSACleanup();
CDialog::OnClose();
}
void CTestDlg::OnBtnSendA()
{
UpdateData(TRUE);
char bufIP[20],bufSend[1024];
CString strMsg;
sockaddr_in receiver;
memset(bufIP,0,sizeof(bufIP));
memset(bufSend,0,sizeof(bufSend));
GetDlgItem(IDC_EDT_IP_B)->GetWindowText(bufIP,20);
receiver.sin_family =AF_INET;
receiver.sin_port =htons(m_nPortB);
receiver.sin_addr.s_addr=inet_addr(bufIP);
strcpy(bufSend,m_sSendA.GetBuffer (1024));
int ret=sendto(sock[0],bufSend,m_sSendA.GetLength (),0,(LPSOCKADDR)&receiver,sizeof(receiver));
if(ret==SOCKET_ERROR)
{
strMsg.Format ("A发送数据失败,错误码:%d",WSAGetLastError());
MessageBox(strMsg,"错误");
return;
}
}
void CTestDlg::OnBtnSendB()
{
UpdateData(TRUE);
char bufIP[20],bufSend[1024];
CString strMsg;
sockaddr_in receiver;
memset(bufIP,0,sizeof(bufIP));
memset(bufSend,0,sizeof(bufSend));
GetDlgItem(IDC_EDT_IP_A)->GetWindowText(bufIP,20);
receiver.sin_family =AF_INET;
receiver.sin_port =htons(m_nPortA);
receiver.sin_addr.s_addr=inet_addr(bufIP);
strcpy(bufSend,m_sSendB.GetBuffer (1024));
int ret=sendto(sock[1],bufSend,m_sSendB.GetLength (),0,(LPSOCKADDR)&receiver,sizeof(receiver));
if(ret==SOCKET_ERROR)
{
strMsg.Format ("B发送数据失败,错误码:%d",WSAGetLastError());
MessageBox(strMsg,"错误");
return;
}
}
void CTestDlg::OnSelchangeComIpA()
{
int index=((CComboBox*)GetDlgItem(IDC_COM_IP_A))->GetCurSel();
POSITION pos=IPList.FindIndex(index);
GetDlgItem(IDC_EDT_IP_A)->SetWindowText(IPList.GetAt(pos));
closesocket(sock[0]);
CreateSocket(0);
}
void CTestDlg::OnSelchangeComIpB()
{
int index=((CComboBox*)GetDlgItem(IDC_COM_IP_B))->GetCurSel();
POSITION pos=IPList.FindIndex(index);
GetDlgItem(IDC_EDT_IP_B)->SetWindowText(IPList.GetAt(pos));
closesocket(sock[1]);
CreateSocket(1);
}
void CTestDlg::OnChangeEdtPortA()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
closesocket(sock[0]);
CreateSocket(0);
}
void CTestDlg::OnChangeEdtPortB()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
closesocket(sock[1]);
CreateSocket(1);
}
BOOL CTestDlg::GetAdapterLists()
{//获取网卡列表
((CComboBox*)GetDlgItem(IDC_COM_IP_A))->Clear();
((CComboBox*)GetDlgItem(IDC_COM_IP_B))->Clear();
CString str;
int index;
DWORD dwStatus;
//函数指针
PGAINFO pGAInfo;
//加载IP Helper API 所需的库文件
HINSTANCE hInst;//实例句柄
hInst=LoadLibrary("iphlpapi.dll");
if(!hInst)
MessageBox("iphlpapi.dll not supported in this platform!\n");
TRACE("net adapters information:\n");
//------------------------------------》获得网卡数据
pGAInfo=(PGAINFO)GetProcAddress(hInst,"GetAdaptersInfo");
PIP_ADAPTER_INFO pInfo=NULL,pInfoTemp=NULL;
ULONG ulSize=0;
dwStatus =pGAInfo(pInfo,&ulSize);//第一次调用,获取缓冲区大小
if (dwStatus == ERROR_BUFFER_OVERFLOW) {
// Allocate a buffer of the appropriate size
if (!(pInfo = (PIP_ADAPTER_INFO)malloc(ulSize))) {
MessageBox("\n Insufficient Memory ");
return false;
}
// Obtain the Adapter Info
dwStatus =pGAInfo(pInfo,&ulSize);
}
if (dwStatus != ERROR_SUCCESS) {//失败
MessageBox("取网卡信息失败!","错误");
return false;
}
pInfoTemp = pInfo;
while(pInfo)
{
//网卡名
str.Format("adapter name:%s\n ",pInfo->AdapterName);
TRACE(str);
//网卡描述信息
str.Format("%s\n",pInfo->Description);
((CComboBox*)GetDlgItem(IDC_COM_IP_A))->AddString(str);
((CComboBox*)GetDlgItem(IDC_COM_IP_B))->AddString(str);
str="description:"+str;
TRACE(str);
//记录下它的索引值以便后面使用。
index=pInfo->Index;
//物理地址的长度
str.Format ("hardware address length: %d\n",pInfo->AddressLength);
TRACE(str);
//显示物理地址
TRACE("hardware address:\n");
CString temp;
str="";
for(int i=0;i<(int)pInfo->AddressLength;i++)
{
temp.Format ("%X",(unsigned int)pInfo->Address[i]);
str=str+=":"+temp;
}
str=str.Right(str.GetLength ()-1);
TRACE(str);
//显示绑定于这张网卡之上的IP地址
TRACE("\nip address bound to this chapter:\n ");
PIP_ADDR_STRING pAddTemp=&(pInfo->IpAddressList);
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
str=pAddTemp->IpAddress.String;
TRACE("\n***********************\n");
TRACE(str);
TRACE("\n*************************\n");
pAddTemp=pAddTemp->Next;
}
IPList.AddTail(str);
//显示当前使用的IP地址
//因为在离线状态下pInfo->CurrentIpAddress被置空,所以有必要做检查
if(pInfo->CurrentIpAddress)
{
TRACE("current ip using:\n");
pAddTemp=pInfo->CurrentIpAddress;
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
str=pAddTemp->IpAddress.String;
TRACE("\n***********************\n");
TRACE(str);
TRACE("\n*************************\n");
pAddTemp=pAddTemp->Next;
}
}
else
TRACE("network malfunctioning,no ip is in use!\n");
//显示DHCP 服务器数据
if(pInfo->DhcpEnabled !=0)
{
TRACE("\nDHCP in use:\n");
pAddTemp=&(pInfo->DhcpServer);
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
str=pAddTemp->IpAddress.String;
TRACE("\n***********************\n");
TRACE(str);
TRACE("\n*************************\n");
pAddTemp=pAddTemp->Next;
}
}
else
{
TRACE("\nDHCP doesn't in use:\n");
}
//将当前指针移向下一个
pInfo=pInfo->Next;
}
delete pInfoTemp;//回收无用内存
//------------------------------------》获得网卡数据部分结束
/*
TRACE("\n");
//函数指针
PGNOINTERFACE pGNOInterface;
PGIINFO pGIInfo;
TRACE("\n---------------\nnetwork interfaces information:\n");
//------------------------------------》网络接口信息部分
//显示网络接口的个数
DWORD ulNumOfInterfaces=0;
pGNOInterface=(PGNOINTERFACE)GetProcAddress(hInst,"GetNumberOfInterfaces");
pGNOInterface(&ulNumOfInterfaces);
str.Format ("U have %d network interfaces\n",ulNumOfInterfaces);
TRACE(str);
//获取网络接口信息
pGIInfo=(PGIINFO)GetProcAddress(hInst,"GetInterfaceInfo");
PIP_INTERFACE_INFO pIInfo=NULL;
ulSize=0;
pGIInfo(pIInfo,&ulSize);//第一次调用,获取缓冲区大小
pIInfo=(PIP_INTERFACE_INFO)new(char[ulSize]);
pGIInfo(pIInfo,&ulSize);
//显示网络接口信息
for(int i=0;i<pIInfo->NumAdapters;i++)
{
//str.Format ("Adapter index:%d\n and name:%s \n",pIInfo->Adapter[i].Index,pIInfo->Adapter[i].Name);
str.Format ("Adapter index:%d\n and name:",pIInfo->Adapter[i].Index);
str+=pIInfo->Adapter[i].Name;
TRACE(str);
TRACE("\n");
}
delete pIInfo;
//------------------------------------》网络接口信息部分结束
TRACE("\n");
//函数指针
PGIAT pGIAT;
TRACE("\n---------------------\nip information:\n");
//------------------------------------》IP绑定信息
pGIAT=(PGIAT)GetProcAddress(hInst,"GetIpAddrTable");
PMIB_IPADDRTABLE pIPTable=NULL;
ulSize=0;
pGIAT(pIPTable,&ulSize,TRUE);//获得缓冲区大小
pIPTable=(PMIB_IPADDRTABLE)new(char[ulSize]);
pGIAT(pIPTable,&ulSize,TRUE);
for(i=0;i< (int)pIPTable->dwNumEntries;i++)
{
//取出每一个字段,显示IP
str.Format ("ip address:%d.%d.%d.%d\n",(unsigned int)((LOWORD(pIPTable->table[i].dwAddr)&0x00FF)),\
(unsigned int)((LOWORD(pIPTable->table[i].dwAddr)>>8)),\
(unsigned int)((HIWORD(pIPTable->table[i].dwAddr)&0x00FF)),\
(unsigned int)((HIWORD(pIPTable->table[i].dwAddr))>>8));
//显示绑定网络接口的索引
TRACE(str);
str.Format("it is bound to interface:%d\n",pIPTable->table[i].dwIndex);
TRACE(str);
//显示子网掩码
str.Format("it's net mask:%d.%d.%d.%d\n",(unsigned int)((LOWORD(pIPTable->table[i].dwMask)&0x00FF)),\
(unsigned int)((LOWORD(pIPTable->table[i].dwMask))>>8),\
(unsigned int)((HIWORD(pIPTable->table[i].dwMask)&0x00FF)),\
(unsigned int)((HIWORD(pIPTable->table[i].dwMask))>>16));
TRACE(str);
//显示广播地址
str.Format("and broadcast addres:%d.%d.%d.%d\n",
(unsigned int)((LOWORD(pIPTable->table[i].dwBCastAddr)&0x00FF)),\
(unsigned int)( (LOWORD (pIPTable->table[i].dwBCastAddr) ) >>8),\
(unsigned int)((HIWORD(pIPTable->table[i].dwBCastAddr)&0x00FF)),\
(unsigned int)((HIWORD(pIPTable->table[i].dwBCastAddr))>>16));
TRACE(str);
//显示最大报文大小
str.Format("it's reassembly size:%d\n\n",pIPTable->table[i].dwReasmSize);
TRACE(str);
}
//------------------------------------》IP绑定信息结束
delete pIPTable;
PAIA pAIA;
//------------------------------------》IP设置部分
TRACE("\n----------------\nIP设置部分\n");
if(index==0)
{
TRACE("\n no adapters available, cannot set ip address!\n");
return 0;//没有网络接口可以设置
}
pAIA=(PAIA)GetProcAddress(hInst,"AddIPAddress");
IPAddr addr=0x184BC5CA;
IPMask mask=0x00FFFFFF;
ULONG context;
ULONG Inst;
pAIA(addr,mask,index,&context,&Inst);
//------------------------------------》IP设置部分结束
TRACE("\n"); */
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -