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 }



正品E路航V700 7寸高清8G 汽车车载GPS导航仪 车用导航测速一体机

转载于:https://www.cnblogs.com/nuaa/archive/2011/12/22/2298495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值