随着计算机网络化的深入,计算机网络编程在程序设计的过程中变得日益重要。许多文章都曾经介绍过用VC++ 进行Socket编程的方法。但由于都是直接利用动态连接库wsock32.dll进行操作,实现比较繁琐。其实,VC++的MFC类库中提供了CAsynCS ocket这样一个套接字类,用它来实现Socket编程,是非常方便的。
客户端
创 建一个Dialog Based项目:CSockClient。设计一对话框,增加ID_Connect(连接)、ID_Send(发送)、ID_Exit(关闭)按钮,增加 ListBox控件IDC_LISTMSG和Edit控件IDC_EDITMSG,并按下表在ClassWizard中为CCSockClientDlg 类添加变量。
Control ID Type Member
IDC_EDITMSG CEdit m_MSG
IDC_LISTMSG ClistBox m_MSGS
在MySock.ccp中添加以下代码:
#include "CSockClient.h"
#include "CSockClientDlg.h"
在MySock.h中添加以下代码:
public:
BOOL m_bConnected;
UINT m_nLength;
char m_szBuffer[4096];
在MySock.ccp中重载各函数
MySock::MySock()
{
m_nLength=0;
memset(m_szBuffer,0,sizeof(m_szBuffer));
m_bConnected=FALSE;}
MySock::~MySock()
{
//关闭套接字
if(m_hSocket!=INVALID_SOCKET)
Close();
}
void MySock::OnReceive(int nErrorCode)
{
m_nLength=Receive(m_szBuffer,sizeof(m_szBuffer),0);
//下面两行代码用来获取对话框指针
CCSockClientApp* pApp=(CCSockClientApp*)AfxGetApp();
CCSockClientDlg* pDlg=(CCSockClientDlg*)pApp- >m_pMainWnd;
pDlg- >m_MSGS.InsertString(0,m_szBuffer);
memset(m_szBuffer,0,sizeof(m_szBuffer));
CAsyncSocket::OnReceive(nErrorCode);
}
void MySock::OnSend(int nErrorCode)
{
Send(m_szBuffer,m_nLength,0);
m_nLength=0;
memset(m_szBuffer,0,sizeof(m_szBuffer));
//继续提请一个“读”的网络事件,接收Server消息
AsyncSelect(FD_READ);
CAsyncSocket::OnSend(nErrorCode);
}
void MySock::OnConnect(int nErrorCode)
{
if (nErrorCode==0)
{
m_bConnected=TRUE;
CCSockClientApp* pApp=(CCSockClientApp*)AfxGetApp();
CCSockClientDlg* pDlg=(CCSockClientDlg*)pApp- >m_pMainWnd;
memcpy(m_szBuffer,"Connected to ",13);
strncat(m_szBuffer,pDlg- >m_szServerAdr,
sizeof(pDlg- >m_szServerAdr));
pDlg- >m_MSGS.InsertString(0,m_szBuffer);
AsyncSelect(FD_READ);
}
//提请一个“读”的网络事件,准备接收
CAsyncSocket::OnConnect(nErrorCode);
}
建一对话框IDD_Addr,用来输入IP地址和Port;增加两个Edit控件:IDC_Addr、IDC_Port按下表在ClassWizard中为CAddrDlg类添加变量。
Control ID Type Member IDC_Addr CString m_Addr IDC_Port Int m_Port 在CSockClientDlg.ccp中添加代码: #include "AddrDlg.h" protected: int TryCount; MySock m_clientSocket; UINT m_szPort; public: char m_szServerAdr[256]; |
m_clientSocket.ShutDown(2); m_clientSocket.m_hSocket=INVALID_SOCKET; m_clientSocket.m_bConnected=FALSE; CAddrDlg m_Dlg; m_Dlg.m_Port=1088;//默认端口1088 if (m_Dlg.DoModal()==IDOK && !m_Dlg.m_Addr.IsEmpty()) { memcpy(m_szServerAdr,m_Dlg.m_Addr,sizeof(m_szServerAdr)); m_szPort=m_Dlg.m_Port; SetTimer(1,1000,NULL);//建立计时器,每1秒尝试连接一次 TryCount=0; } |
void CCSockClientDlg::OnTimer(UINT nIDEvent) |
void CCSockClientDlg::OnSend() { if (m_clientSocket.m_bConnected) { m_clientSocket.m_nLength=m_MSG.GetWindowText(m_clientSocket.m_szBuffer, sizeof(m_clientSocket.m_szBuffer)); m_clientSocket.AsyncSelect(FD_WRITE); m_MSG.SetWindowText(""); } } 双击IDD_CSOCKCLIENT_DIALOG对话框中的“关闭”按钮,添加以下代码: void CCSockClientDlg::OnExit() { m_clientSocket.ShutDown(2); //关闭Socket EndDialog(0); } |
CNewSocket::OnReceive(int nErrorCOde) { m_nLength=Receive(m_szBuffer,sizeof(m_szBuffer),0); AsyncSelect(FD_WRITE);}// 直接转发消息 CNewSocket::OnSend(int nErrorCode) { Send(m_szBuffer,m_nLength,0); } } |
public:: CNewSocket* m_pSocket; void CMyServerSocket::OnAccept(int nErrorCode) { CNewSocket* pSocket = new CNewSocket(); if (Accept(*pSocket)) { pSocket- >AsyncSelect(FD_READ); m_pSocket=pSocket; }else delete pSocket; } |
public: CMyServerSocket m_srvrSocket; void CCSockServerDlg::OnListen() { if (m_srvrSocket.m_hSocket==INVALID_SOCKET) { BOOL bFlag=m_srvrSocket.Create(UserPort,SOCK_STREAM,FD_ACCEPT); if (!bFlag) { AfxMessageBox(“Socket Error!”); M_srvrSocket.Close(); PostQuitMessage(0); Return; } } if (!m_srvrSocket。Listen(1)) { int nErrorCode = m_srvrSocket.GetLastError(); if (nError!=WSAEWOULDBLOCK) { AfxMessageBox(“Socket Error!”); M_srvrSocket.Close(); PostQuitMessage(0); Return; } } } |