本程序在同一线程内实现了接收端和发送端,与上一章多线程聊天室 的结果类似
在编写网络程序时,采用异步选择机制可能提高网络应用程序的性能,如果再配合多线程,将再进一步提高程序性能
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
<span style="font-size:18px;">BOOL CMy0627AApp::InitInstance()
{
CWinApp::InitInstance();
//<strong><span style="color:#cc0000;">加载套接字库并进行版本协商</span></strong>
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested=MAKEWORD(2,2);
err=::WSAStartup(wVersionRequested,&wsaData); //调用成功返回值是0
//::AfxSocketInit(); 加载的是1.1版本库 WSAStartup加载的是2.0版本
if(err!=0)
{
return FALSE;
}
if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=2)
{
::WSACleanup();
}</span>
<span style="font-size:18px;">private:
SOCKET m_socket;
public:
BOOL InitSocket(void);
protected:
afx_msg LRESULT OnUmSock(WPARAM wParam, LPARAM lParam);
public:
afx_msg void OnBnClickedOk();
afx_msg void OnDestroy();</span>
<span style="font-size:18px;">#define UM_SOCK WM_USER+101</span>
<span style="font-size:18px;">BOOL CMy0627ADlg::InitSocket(void)
{
this->m_socket=::WSASocketA(AF_INET,SOCK_DGRAM,0,NULL,0,0);
if(INVALID_SOCKET==this->m_socket)
{
::AfxMessageBox("创建套接字失败");
return FALSE;
}
SOCKADDR_IN addrSock;//地址结构体
addrSock.sin_family=AF_INET;//地址族
addrSock.sin_port=::htons(6000);//端口
addrSock.sin_addr.S_un.S_addr=::htonl(INADDR_ANY);//收发本地任意IP
int retval;
//绑定套接字
retval=::bind(this->m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));
//与指定的IP和端口绑定
if(SOCKET_ERROR==retval)
{
::closesocket(this->m_socket);
::AfxMessageBox("绑定失败");
return FALSE;
}
<strong><span style="color:#cc0000;">retval=::WSAAsyncSelect(this->m_socket,this->m_hWnd,UM_SOCK,FD_READ);
//请求一个其于Windows消息的网络事件通知</span></strong>
//一旦有数据来,就会触发FD_READ事件,通过自定义消息UM_SOCK来调用线程
if(SOCKET_ERROR==retval)
{
::AfxMessageBox("注册网络读取事件失败");
return FALSE;
}
return TRUE;
}</span>
<span style="font-size:18px;">BEGIN_MESSAGE_MAP(CMy0627ADlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
<strong>ON_MESSAGE(UM_SOCK, &CMy0627ADlg::OnUmSock)</strong>
ON_BN_CLICKED(IDOK, &CMy0627ADlg::OnBnClickedOk)
ON_WM_DESTROY()
END_MESSAGE_MAP()</span>
<span style="font-size:18px;">afx_msg LRESULT CMy0627ADlg::OnUmSock(WPARAM wParam, LPARAM lParam)
{
switch(LOWORD(lParam))
{
case FD_READ:
WSABUF wsabuf;
wsabuf.buf=new char[200];
wsabuf.len=200;
DWORD dwRead;
DWORD dwFlag=0;
SOCKADDR_IN addrFrom;
int len=sizeof(SOCKADDR);
CString str;
CString strTemp;
int retval=::WSARecvFrom(this->m_socket,&wsabuf,1,&dwRead,&dwFlag,
(SOCKADDR*)&addrFrom,&len,NULL,NULL);
if(SOCKET_ERROR==retval)
{
::AfxMessageBox("接收数据失败");
delete[] wsabuf.buf;
return 0;
}
str.Format("%s 说 :%s",::inet_ntoa(addrFrom.sin_addr),wsabuf.buf);
str+="\r\n";
this->GetDlgItemTextA(IDC_EDIT1,strTemp);
str+=strTemp;
this->SetDlgItemTextA(IDC_EDIT1,str);
delete[]wsabuf.buf;
break;
}
return 0;
}</span>
<span style="font-size:18px;">void CMy0627ADlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
//CDialogEx::OnOK();
DWORD dwIP; //主机字节顺序的地址 随后会转成网络字节顺序的地址
CString strSend;
WSABUF wsabuf; //保存将要发送的数据和长度
DWORD dwSend;
int len;
SOCKADDR_IN addrTo; //定义地址结构体对象 并设置其成员
((CIPAddressCtrl*)this->GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP); //得到IP地址
addrTo.sin_addr.S_un.S_addr=::htonl(dwIP); //对方的IP地址
addrTo.sin_family=AF_INET;
addrTo.sin_port=::htons(6000);
this->GetDlgItemTextA(IDC_EDIT2,strSend);
len=strSend.GetLength();
wsabuf.buf=strSend.GetBuffer(len); //<strong>CString 转char*</strong>
wsabuf.len=len+1; //为了末尾的'\0'
int err;
<strong><span style="color:#cc0000;">err=::WSASendTo(this->m_socket,&wsabuf,1,&dwSend,0,(SOCKADDR*)&addrTo,sizeof(SOCKADDR),NULL,NULL);</span></strong>
if(SOCKET_ERROR==err)
{
::AfxMessageBox("发送数据失败");
return;
}
this->SetDlgItemTextA(IDC_EDIT2,"");
}
void CMy0627ADlg::OnDestroy()
{
CDialogEx::OnDestroy();
// TODO: Add your message handler code here
//::AfxMessageBox("进入WM_DESTROY");
if(this->m_socket)
{
::closesocket(this->m_socket);
}
::WSACleanup();
}</span>