2.使用MFC的CSocket类建立简单的UDP通信
2017-06-05 16:30
489 查看
接收端:
// csocketReceive.cpp : 定义控制台应用程序的入口点。
//
//接收端 右键项目属性 MFC的使用改成共享 dll.
#include "stdafx.h"
#include <afxsock.h>
using namespace std;
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
AfxSocketInit();
CSocket sock;
bool b = sock.Create(8888, SOCK_DGRAM);//socket bind绑定端口
if (!b)
{
cout << GetLastError() << endl;
return -1;
}
char s[2048];
int n = 0;
CString szIP;
UINT nPort;
while ((n = sock.Receive(s,sizeof(s))) > 0)
{
if (s[0] == '#')
break;
cout << "IP是:" << (LPCTSTR)szIP << "端口是:" << nPort << endl;
s
= 0;
cout << s << endl;
}
return 0;
}
发送端:
// csocketReceive.cpp : 定义控制台应用程序的入口点。
//
//接收端 右键项目属性 MFC的使用改成共享 dll.
#include "stdafx.h"
#include <afxsock.h>
using namespace std;
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
AfxSocketInit();
CSocket sock;
bool b = sock.Create(8888, SOCK_DGRAM);//socket bind绑定端口
if (!b)
{
cout << GetLastError() << endl;
return -1;
}
char s[2048];
int n = 0;
CString szIP;
UINT nPort;
while ((n = sock.Receive(s,sizeof(s))) > 0)
{
if (s[0] == '#')
break;
cout << "IP是:" << (LPCTSTR)szIP << "端口是:" << nPort << endl;
s
= 0;
cout << s << endl;
}
return 0;
}
发送端:
// csocket.cpp : 定义控制台应用程序的入口点。 // //发送端 右键项目属性 MFC的使用改成共享dll mfc //使用MFC的CSocket类实现UDP通信 #include "stdafx.h" #include <afxsock.h> using namespace std; #include <iostream> int _tmain(int argc,_TCHAR* argv[]) { AfxSocketInit(); CSocket sock; BOOL b = sock.Create(0,SOCK_DGRAM);//绑定自己的端口 if (!b) { cout << GetLastError() << endl; } char s[2048]; while (true) { cin >> s; sock.SendTo(s, strlen(s), 8888, _T("192.168.1.1"));//接收的端口和IP地址 _T()表示Unicode的意思 if(s[0] == '#') break; } return 0; }
相关文章推荐
- MFC中利用CSocket实现UDP通信
- MFC中利用CSocket实现UDP通信
- 使用UDP套接字实现简单的通信
- C#使用 UdpClient 类进行简单通信的例子
- 简单的,使用套接字实UDP通信(循环服务器)
- JAVA系列课程讲座二:使用Socket通信实现简单聊天通信程序(UDP方式)
- java使用UDP来进行客户端和服务器端通信的简单例子
- 【MFC】利用CSOCKET实现连接、简单通信
- MFC中利用CSocket实现UDP通信
- C#使用UdpClient类进行简单通信的实例
- 简单使用SOCKET,TCP,UDP模式之间的通信
- [MFC]UDP通信的简单实现
- [转] 最简单的使用UDP通信的Python Socket例子
- java使用UDP来进行客户端和服务器端通信的简单例子
- 使用ArcGIS Server和ASP.net建立一个简单的网站
- 利用MFC的Csocket类实现网络通信
- 【翻译】使用ASP.NET MVC 和LINQ建立一个简单的博客 - Part 3
- 用Prof-UIS在VS中建立最简单的MFC程序
- 用Prof-UIS在VS中建立最简单的MFC程序
- MFC控件 状态条的简单介绍及使用 CStatusBar