#include <afxcmn.h> // MFC support for Windows Common Controls
#include<Afxsock.h>
#endif // _AFX_NO_AFXCMN_SUPPORT//已在stdafx.h中包含<Afxsock.h>BOOL CQqchatApp::InitInstance()
{
AfxEnableControlContainer();
if (!AfxSocketInit())
{
AfxMessageBox("创建套接字失败!");
return FALSE;
}\\创建套接字 // Generated message map functions
//{{AFX_MSG(CQqchatDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnBtnSend();
//}}AFX_MSG
afx_msg void OnSock(WPARAM,LPARAM);\\函数原型声明
DECLARE_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CQqchatDlg, CDialog)
//{{AFX_MSG_MAP(CQqchatDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
//}}AFX_MSG_MAP
ON_MESSAGE(UM_SOCK,OnSock)\\消息映射
END_MESSAGE_MAP()
BOOL CQqchatDlg::initsocket()
{
m_socket=socket(AF_INET,SOCK_DGRAM,0);
if(m_socket==INVALID_SOCKET)
{
MessageBox("创建套接字失败!");
return FALSE;}SOCKADDR_IN addrsock;
int revtal;
int len=sizeof(SOCKADDR);
unsigned short port1=(unsigned short) GetDlgItemInt(IDC_EDIT_PORT1);
addrsock.sin_family=AF_INET;
addrsock.sin_port=htons(port1);
addrsock.sin_addr .S_un .S_addr =htonl(INADDR_ANY);//套接字的所有成员量都已初始化后才可进行绑定;
revtal=bind(m_socket,(SOCKADDR*)&addrsock,len);
if(revtal==SOCKET_ERROR)
{
MessageBox("绑定套接字失败");
return FALSE;
}
if(SOCKET_ERROR==WSAAsyncSelect(m_socket,m_hWnd,UM_SOCK,FD_READ))
{
MessageBox("网络事件读取失败!");
return FALSE;}
return TRUE;}
void CQqchatDlg::OnSock (WPARAM wParam,LPARAM lParam)
{
switch(LOWORD(lParam))
{case FD_READ:
int len=sizeof(SOCKADDR);
SOCKADDR_IN addrfrom;
char tempbuff [200];
if(recvfrom(m_socket,tempbuff,200,0,(SOCKADDR*)&addrfrom,&len)==SOCKET_ERROR)
{
MessageBox("接受数据失败!"); return;
}
CString strRecv;
strRecv.Format ("%s 说:%s",inet_ntoa(addrfrom.sin_addr),tempbuff);
SetDlgItemText(IDC_EDIT_RECV,tempbuff);
break;
} }void CQqchatDlg::OnBtnSend()
{
// TODO: Add your control notification handler code here
DWORD dwIP1;
DWORD dwIP2;
((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP1);
((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP2); SOCKADDR_IN addrtoz;
SOCKADDR_IN addrtoy;
unsigned short port1=0;
unsigned short port2=0;
port1=(unsigned short)GetDlgItemInt(IDC_EDIT_PORT1,NULL,FALSE); port1=(unsigned short)GetDlgItemInt(IDC_EDIT_PORT2,NULL,FALSE); addrtoz.sin_port=htons(port1);
addrtoz.sin_family =AF_INET;
addrtoz.sin_addr.S_un .S_addr =htonl(dwIP1);
addrtoy.sin_port=htons(port2);
addrtoy.sin_family=AF_INET;
addrtoy.sin_addr .S_un .S_addr =htonl(dwIP2);
CString str;
GetDlgItemText(IDC_EDIT_SEND,str); if( sendto(m_socket,str,str.GetLength()+1,0,(SOCKADDR*)&addrtoy,sizeof(SOCKADDR))==SOCKET_ERROR)
{
MessageBox("发送他机数据失败!");
return;
}
if (sendto(m_socket,str,str.GetLength ()+1,0,(SOCKADDR*)&addrtoz,sizeof(SOCKADDR))==SOCKET_ERROR)
{MessageBox("发送本机数据失败!");
return;
}
SetDlgItemText(IDC_EDIT_SEND," ");
}
#include<Afxsock.h>
#endif // _AFX_NO_AFXCMN_SUPPORT//已在stdafx.h中包含<Afxsock.h>BOOL CQqchatApp::InitInstance()
{
AfxEnableControlContainer();
if (!AfxSocketInit())
{
AfxMessageBox("创建套接字失败!");
return FALSE;
}\\创建套接字 // Generated message map functions
//{{AFX_MSG(CQqchatDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnBtnSend();
//}}AFX_MSG
afx_msg void OnSock(WPARAM,LPARAM);\\函数原型声明
DECLARE_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CQqchatDlg, CDialog)
//{{AFX_MSG_MAP(CQqchatDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
//}}AFX_MSG_MAP
ON_MESSAGE(UM_SOCK,OnSock)\\消息映射
END_MESSAGE_MAP()
BOOL CQqchatDlg::initsocket()
{
m_socket=socket(AF_INET,SOCK_DGRAM,0);
if(m_socket==INVALID_SOCKET)
{
MessageBox("创建套接字失败!");
return FALSE;}SOCKADDR_IN addrsock;
int revtal;
int len=sizeof(SOCKADDR);
unsigned short port1=(unsigned short) GetDlgItemInt(IDC_EDIT_PORT1);
addrsock.sin_family=AF_INET;
addrsock.sin_port=htons(port1);
addrsock.sin_addr .S_un .S_addr =htonl(INADDR_ANY);//套接字的所有成员量都已初始化后才可进行绑定;
revtal=bind(m_socket,(SOCKADDR*)&addrsock,len);
if(revtal==SOCKET_ERROR)
{
MessageBox("绑定套接字失败");
return FALSE;
}
if(SOCKET_ERROR==WSAAsyncSelect(m_socket,m_hWnd,UM_SOCK,FD_READ))
{
MessageBox("网络事件读取失败!");
return FALSE;}
return TRUE;}
void CQqchatDlg::OnSock (WPARAM wParam,LPARAM lParam)
{
switch(LOWORD(lParam))
{case FD_READ:
int len=sizeof(SOCKADDR);
SOCKADDR_IN addrfrom;
char tempbuff [200];
if(recvfrom(m_socket,tempbuff,200,0,(SOCKADDR*)&addrfrom,&len)==SOCKET_ERROR)
{
MessageBox("接受数据失败!"); return;
}
CString strRecv;
strRecv.Format ("%s 说:%s",inet_ntoa(addrfrom.sin_addr),tempbuff);
SetDlgItemText(IDC_EDIT_RECV,tempbuff);
break;
} }void CQqchatDlg::OnBtnSend()
{
// TODO: Add your control notification handler code here
DWORD dwIP1;
DWORD dwIP2;
((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP1);
((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP2); SOCKADDR_IN addrtoz;
SOCKADDR_IN addrtoy;
unsigned short port1=0;
unsigned short port2=0;
port1=(unsigned short)GetDlgItemInt(IDC_EDIT_PORT1,NULL,FALSE); port1=(unsigned short)GetDlgItemInt(IDC_EDIT_PORT2,NULL,FALSE); addrtoz.sin_port=htons(port1);
addrtoz.sin_family =AF_INET;
addrtoz.sin_addr.S_un .S_addr =htonl(dwIP1);
addrtoy.sin_port=htons(port2);
addrtoy.sin_family=AF_INET;
addrtoy.sin_addr .S_un .S_addr =htonl(dwIP2);
CString str;
GetDlgItemText(IDC_EDIT_SEND,str); if( sendto(m_socket,str,str.GetLength()+1,0,(SOCKADDR*)&addrtoy,sizeof(SOCKADDR))==SOCKET_ERROR)
{
MessageBox("发送他机数据失败!");
return;
}
if (sendto(m_socket,str,str.GetLength ()+1,0,(SOCKADDR*)&addrtoz,sizeof(SOCKADDR))==SOCKET_ERROR)
{MessageBox("发送本机数据失败!");
return;
}
SetDlgItemText(IDC_EDIT_SEND," ");
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货