CAsyncSocket进行TCP通行


服务端代码:

CNewSocket类

 1 class CNewSocket : public CAsyncSocket
2 {
3 public:
4 UINT m_nLength;
5 char m_szBuffer[4096];
6
7 public:
8 CNewSocket();
9 virtual ~CNewSocket();
10 public:
11 // ClassWizard generated virtual function overrides
12 //{{AFX_VIRTUAL(CNewSocket)
13 public:
14 virtual void OnReceive(int nErrorCode);
15 virtual void OnSend(int nErrorCode);
16 //}}AFX_VIRTUAL
17
18 // Generated message map functions
19 //{{AFX_MSG(CNewSocket)
20 // NOTE - the ClassWizard will add and remove member functions here.
21 //}}AFX_MSG
22
23 // Implementation
24 protected:
25 };

 

 1 CNewSocket::CNewSocket()
2 {
3 m_nLength=0;
4 memset(m_szBuffer,0,sizeof(m_szBuffer));
5 }
6
7 CNewSocket::~CNewSocket()
8 {
9 if (m_hSocket!=INVALID_SOCKET)
10 Close();
11 }
12
13
14 // Do not edit the following lines, which are needed by ClassWizard.
15 #if 0
16 BEGIN_MESSAGE_MAP(CNewSocket, CAsyncSocket)
17 //{{AFX_MSG_MAP(CNewSocket)
18 //}}AFX_MSG_MAP
19 END_MESSAGE_MAP()
20 #endif // 0
21
22 /////////////////////////////////////////////////////////////////////////////
23 // CNewSocket member functions
24
25 void CNewSocket::OnReceive(int nErrorCode)
26 {
27 // TODO: Add your specialized code here and/or call the base class
28 m_nLength=Receive(m_szBuffer,sizeof(m_szBuffer),0);
29 AsyncSelect(FD_WRITE);
30 CAsyncSocket::OnReceive(nErrorCode);
31 }
32
33 void CNewSocket::OnSend(int nErrorCode)
34 {
35 // TODO: Add your specialized code here and/or call the base class
36 char m_sendBuffer[4096];
37 memcpy(m_sendBuffer,"服务器转发:",24);
38 strncat(m_sendBuffer,m_szBuffer,m_nLength);
39 Send(m_sendBuffer,sizeof(m_sendBuffer));
40 AsyncSelect(FD_READ);
41 CAsyncSocket::OnSend(nErrorCode);
42 }

CListenSocket类

 1 #include "NewSocket.h"
2 class CListenSocket : public CAsyncSocket
3 {
4 public:
5 CNewSocket* m_pSocket;
6 public:
7 CListenSocket();
8 virtual ~CListenSocket();
9 public:
10 // ClassWizard generated virtual function overrides
11 //{{AFX_VIRTUAL(CListenSocket)
12 public:
13 virtual void OnAccept(int nErrorCode);
14 //}}AFX_VIRTUAL
15
16 // Generated message map functions
17 //{{AFX_MSG(CListenSocket)
18 // NOTE - the ClassWizard will add and remove member functions here.
19 //}}AFX_MSG
20
21 // Implementation
22 protected:
23 };

 

 1 // ListenSocket.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "SocketTCPServer.h"
6 #include "ListenSocket.h"
7
8 #ifdef _DEBUG
9 #define new DEBUG_NEW
10 #undef THIS_FILE
11 static char THIS_FILE[] = __FILE__;
12 #endif
13
14 /////////////////////////////////////////////////////////////////////////////
15 // CListenSocket
16
17 CListenSocket::CListenSocket()
18 {
19 }
20
21 CListenSocket::~CListenSocket()
22 {
23 }
24
25
26 // Do not edit the following lines, which are needed by ClassWizard.
27 #if 0
28 BEGIN_MESSAGE_MAP(CListenSocket, CAsyncSocket)
29 //{{AFX_MSG_MAP(CListenSocket)
30 //}}AFX_MSG_MAP
31 END_MESSAGE_MAP()
32 #endif // 0
33
34 /////////////////////////////////////////////////////////////////////////////
35 // CListenSocket member functions
36
37 void CListenSocket::OnAccept(int nErrorCode)
38 {
39 // TODO: Add your specialized code here and/or call the base class
40 CNewSocket* pSocket=new CNewSocket;
41 if (Accept(*pSocket))
42 {
43 pSocket->AsyncSelect(FD_READ);//让pSocket准备接受数据
44 m_pSocket=pSocket;
45 }
46 else
47 delete pSocket;
48 CAsyncSocket::OnAccept(nErrorCode);
49 }
 1 #include "ListenSocket.h"
2
3 class CSocketTCPServerDlg : public CDialog
4 {
5 // Construction
6 public:
7 CSocketTCPServerDlg(CWnd* pParent = NULL); // standard constructor
8 CListenSocket m_srvSocket;
9 // Dialog Data
10 //{{AFX_DATA(CSocketTCPServerDlg)
11 enum { IDD = IDD_SOCKETTCPSERVER_DIALOG };
12 // NOTE: the ClassWizard will add data members here
13 //}}AFX_DATA
14
15 // ClassWizard generated virtual function overrides
16 //{{AFX_VIRTUAL(CSocketTCPServerDlg)
17 protected:
18 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
19 //}}AFX_VIRTUAL
20
21 // Implementation
22 protected:
23 HICON m_hIcon;
24
25 // Generated message map functions
26 //{{AFX_MSG(CSocketTCPServerDlg)
27 virtual BOOL OnInitDialog();
28 afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
29 afx_msg void OnPaint();
30 afx_msg HCURSOR OnQueryDragIcon();
31 afx_msg void OnStart();
32 //}}AFX_MSG
33 DECLARE_MESSAGE_MAP()
34 };



void CSocketTCPServerDlg::OnStart() 
{
// TODO: Add your control notification handler code here
if (m_srvSocket.m_hSocket==INVALID_SOCKET)
{
BOOL bFlag=m_srvSocket.Create(5088,SOCK_STREAM,FD_ACCEPT|FD_READ);//只触发Accept事件,当收到connect请求调用OnAccept
if (!bFlag)
{
AfxMessageBox("套接字创建失败");
m_srvSocket.Close();
PostQuitMessage(0);
return;
}
GetDlgItem(IDC_START)->EnableWindow(FALSE);
}
if (!m_srvSocket.Listen(5))
{
int nErrorCode=m_srvSocket.GetLastError();
if (nErrorCode!=WSAEWOULDBLOCK)//不是线程被阻塞
{
AfxMessageBox("套接字错误");
m_srvSocket.Close();
PostQuitMessage(0);
return;
}
}
}

 

客户端代码:

CClientSocket

 1 class CClientSocket : public CAsyncSocket
2 {
3 // Attributes
4 public:
5 BOOL m_bConnected;
6 UINT m_nLength;
7 char m_szBuffer[4096];
8 // Operations
9 public:
10 CClientSocket();
11 virtual ~CClientSocket();
12
13 // Overrides
14 public:
15 // ClassWizard generated virtual function overrides
16 //{{AFX_VIRTUAL(CClientSocket)
17 public:
18 virtual void OnConnect(int nErrorCode);
19 virtual void OnSend(int nErrorCode);
20 virtual void OnReceive(int nErrorCode);
21 //}}AFX_VIRTUAL
22
23 // Generated message map functions
24 //{{AFX_MSG(CClientSocket)
25 // NOTE - the ClassWizard will add and remove member functions here.
26 //}}AFX_MSG
27
28 // Implementation
29 protected:
30 };

 

 1 // ClientSocket.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "SocketTCPClient.h"
6 #include "ClientSocket.h"
7 #include "SocketTCPClientDlg.h"
8
9 #ifdef _DEBUG
10 #define new DEBUG_NEW
11 #undef THIS_FILE
12 static char THIS_FILE[] = __FILE__;
13 #endif
14
15 /////////////////////////////////////////////////////////////////////////////
16 // CClientSocket
17
18 CClientSocket::CClientSocket()
19 {
20 m_nLength=0;
21 m_bConnected=FALSE;
22 memset(m_szBuffer,0,sizeof(m_szBuffer));
23 }
24
25 CClientSocket::~CClientSocket()
26 {
27 if (m_hSocket!=INVALID_SOCKET)
28 {
29 Close();
30 }
31 }
32
33
34 // Do not edit the following lines, which are needed by ClassWizard.
35 #if 0
36 BEGIN_MESSAGE_MAP(CClientSocket, CAsyncSocket)
37 //{{AFX_MSG_MAP(CClientSocket)
38 //}}AFX_MSG_MAP
39 END_MESSAGE_MAP()
40 #endif // 0
41
42 /////////////////////////////////////////////////////////////////////////////
43 // CClientSocket member functions
44
45 void CClientSocket::OnConnect(int nErrorCode)
46 {
47 // TODO: Add your specialized code here and/or call the base class
48 if (nErrorCode==0)
49 {
50 m_bConnected=TRUE;
51 CSocketTCPClientApp* pApp=(CSocketTCPClientApp*)AfxGetApp();
52 CSocketTCPClientDlg* pDlg=(CSocketTCPClientDlg*)pApp->m_pMainWnd;
53 memcpy(m_szBuffer,"连接到:",13);
54 strncat(m_szBuffer,pDlg->m_szServerAdr,sizeof(pDlg->m_szServerAdr));
55 pDlg->m_MsgR.InsertString(0,m_szBuffer);
56 pDlg->GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
57 //AsyncSelect(FD_READ);//触发Recv事件,让socket准备接受数据
58 }
59 CAsyncSocket::OnConnect(nErrorCode);
60 }
61
62 void CClientSocket::OnSend(int nErrorCode)
63 {
64 // TODO: Add your specialized code here and/or call the base class
65 Send(m_szBuffer,m_nLength,0);
66 m_nLength=0;
67 memset(m_szBuffer,0,sizeof(m_szBuffer));
68 AsyncSelect(FD_READ);//触发Recv事件,让socket准备接受数据
69 CAsyncSocket::OnSend(nErrorCode);
70 }
71
72 void CClientSocket::OnReceive(int nErrorCode)
73 {
74 // TODO: Add your specialized code here and/or call the base class
75 m_nLength=Receive(m_szBuffer,sizeof(m_szBuffer),0);
76 CSocketTCPClientApp* pApp=(CSocketTCPClientApp*)AfxGetApp();
77 CSocketTCPClientDlg* pDlg=(CSocketTCPClientDlg*)pApp->m_pMainWnd;
78 pDlg->m_MsgR.InsertString(0,m_szBuffer);
79 memset(m_szBuffer,0,sizeof(m_szBuffer));
80 CAsyncSocket::OnReceive(nErrorCode);
81 }

 

 1 void CSocketTCPClientDlg::OnConnect() 
2 {
3 // TODO: Add your control notification handler code here
4 m_clientSocket.ShutDown(2);
5 m_clientSocket.m_hSocket=INVALID_SOCKET;
6 m_clientSocket.m_bConnected=FALSE;
7 CAddrDlg dlg;
8 m_szPort=5088;
9 if (dlg.DoModal()==IDOK&&!dlg.m_Addr.IsEmpty())
10 {
11 memcpy(m_szServerAdr,dlg.m_Addr,sizeof(m_szServerAdr));
12 SetTimer(1,1000,NULL);
13 Trycount=0;
14 }
15 }
16
17 void CSocketTCPClientDlg::OnSend()
18 {
19 // TODO: Add your control notification handler code here
20 if (m_clientSocket.m_bConnected)
21 {
22 m_clientSocket.m_nLength=m_MsgS.GetWindowText(m_clientSocket.m_szBuffer,sizeof(m_clientSocket.m_szBuffer));
23 m_clientSocket.AsyncSelect(FD_WRITE);
24 m_MsgS.SetWindowText("");
25 }
26 }
27
28 void CSocketTCPClientDlg::OnClose()
29 {
30 // TODO: Add your control notification handler code here
31 m_clientSocket.ShutDown(2);
32 EndDialog(0);
33 }
34
35 void CSocketTCPClientDlg::OnTimer(UINT nIDEvent)
36 {
37 // TODO: Add your message handler code here and/or call default
38 if (m_clientSocket.m_hSocket==INVALID_SOCKET)
39 {
40 bool bFlag=m_clientSocket.Create(0,SOCK_STREAM,FD_CONNECT);//只触发CONNECT事件,只触发一次
41 if (!bFlag)
42 {
43 AfxMessageBox("套接字创建失败");
44 m_clientSocket.Close();
45 PostQuitMessage(0);
46 return;
47 }
48 }
49 m_clientSocket.Connect(m_szServerAdr,m_szPort);
50 Trycount++;
51 if (Trycount>=10||m_clientSocket.m_bConnected)
52 {
53 KillTimer(1);
54 if(Trycount>=10)
55 {
56 GetDlgItem(IDC_CONNECT)->EnableWindow(TRUE);
57 }
58 return;
59 }
60 CDialog::OnTimer(nIDEvent);
61 }




posted on 2011-12-22 19:19  Sunny_NUAA  阅读(1066)  评论(0编辑  收藏  举报

导航