CSocket进行UDP通信
客户端:
MySocket类
1 class MySocket : public CSocket
2 {
3 public:
4 MySocket();
5 virtual ~MySocket();
6 SOCKADDR_IN ClientAddr;
7 public:
8 // ClassWizard generated virtual function overrides
9 //{{AFX_VIRTUAL(MySocket)
10 public:
11 virtual void OnReceive(int nErrorCode);
12 virtual void OnSend(int nErrorCode);
13 //}}AFX_VIRTUAL
14
15 // Generated message map functions
16 //{{AFX_MSG(MySocket)
17 // NOTE - the ClassWizard will add and remove member functions here.
18 //}}AFX_MSG
19 protected:
20 };
21
22 #include "stdafx.h"
23 #include "C.h"
24 #include "MySocket.h"
25 #include "CDlg.h"
26 #ifdef _DEBUG
27 #define new DEBUG_NEW
28 #undef THIS_FILE
29 static char THIS_FILE[] = __FILE__;
30 #endif
31
32 /////////////////////////////////////////////////////////////////////////////
33 // MySocket
34
35 MySocket::MySocket()
36 {
37 }
38
39 MySocket::~MySocket()
40 {
41 if (m_hSocket!=INVALID_SOCKET)
42 {
43 Close();
44 }
45 }
46
47
48 // Do not edit the following lines, which are needed by ClassWizard.
49 #if 0
50 BEGIN_MESSAGE_MAP(MySocket, CSocket)
51 //{{AFX_MSG_MAP(MySocket)
52 //}}AFX_MSG_MAP
53 END_MESSAGE_MAP()
54 #endif // 0
55
56 /////////////////////////////////////////////////////////////////////////////
57 // MySocket member functions
58
59 void MySocket::OnReceive(int nErrorCode)
60 {
61 // TODO: Add your specialized code here and/or call the base class
62 char recBuf[1024]={0};
63 int len=sizeof(SOCKADDR_IN);
64
65 CCDlg* dlg=(CCDlg*)AfxGetApp()->GetMainWnd();
66 ReceiveFrom(recBuf,1024,(SOCKADDR*)&ClientAddr,&len,0);
67 dlg->m_msg.SetWindowText(recBuf);
68 CSocket::OnReceive(nErrorCode);
69 }
70
71 void MySocket::OnSend(int nErrorCode)
72 {
73 // TODO: Add your specialized code here and/or call the base class
74 MessageBox(NULL,"","Client Send",0);
75 SendTo("23",2,6901,"127.0.0.1",0);
76 CSocket::OnSend(nErrorCode);
77 }
1 void CCDlg::OnButton2()
2 {
3 // TODO: Add your control notification handler code here
4 m_clientSocket.Create(6900,SOCK_DGRAM,NULL);
5 m_clientSocket.Bind(6900,"127.0.0.1");
6 GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
7 }
8
9 void CCDlg::OnClose()
10 {
11 // TODO: Add your message handler code here and/or call default
12 m_clientSocket.ShutDown(2);
13 CDialog::OnClose();
14 }
服务端代码:
MySocket
1 class MySocket : public CSocket
2 {
3 public:
4 MySocket();
5 virtual ~MySocket();
6 public:
7 // ClassWizard generated virtual function overrides
8 //{{AFX_VIRTUAL(MySocket)
9 public:
10 virtual void OnSend(int nErrorCode);
11 virtual void OnReceive(int nErrorCode);
12 //}}AFX_VIRTUAL
13 // Generated message map functions
14 //{{AFX_MSG(MySocket)
15 // NOTE - the ClassWizard will add and remove member functions here.
16 //}}AFX_MSG
17 protected:
18 };
19
20 void MySocket::OnSend(int nErrorCode)
21 {
22 // TODO: Add your specialized code here and/or call the base class
23 MessageBox(NULL,"服务器发送","",0);
24 CSocket::OnSend(nErrorCode);
25 }
26
27 void MySocket::OnReceive(int nErrorCode)
28 {
29 // TODO: Add your specialized code here and/or call the base class
30 MessageBox(NULL,"服务器接收","",0);
31 CSocket::OnReceive(nErrorCode);
32 }
1 m_socketSend.Create(6901,SOCK_DGRAM,NULL);
2 m_socketSend.Bind(6901,"127.0.0.1");
3
4
5
6 void CSDlg::OnButton1()
7 {
8 // TODO: Add your control notification handler code here
9 UpdateData();
10 char *msgSend=m_msg.GetBuffer(m_msg.GetLength());
11 m_socketSend.SendTo(msgSend,m_msg.GetLength(),6900,"127.0.0.1",0);
12 }
13
14 void CSDlg::OnClose()
15 {
16 // TODO: Add your message handler code here and/or call default
17 m_socketSend.ShutDown(2);
18 CDialog::OnClose();
19 }
posted on 2011-12-22 21:31 Sunny_NUAA 阅读(9846) 评论(0) 编辑 收藏 举报