当前位置:网站首页>Vsual studio 2013 environment UDP multicast
Vsual studio 2013 environment UDP multicast
2022-07-24 05:59:00 【Joey Boye o (* ^ ^ ^ *) o】
Preface `
MFC(Microsoft Foundation Classes) Is the abbreviation of Microsoft basic class library , It's a Microsoft implementation of c++ Class library , It mainly encapsulates most of the windows API function ,vc++ It was developed by Microsoft c/c++ Integrated development environment , The so-called integrated development environment , That is to say, it can be used to edit , compile , debugging , Instead of using multiple tools to rotate operations , More flexibility .vc It also refers to its internal compiler , The IDE must have a compiler kernel , for example DevC++ One of the compiler cores is gcc. MFC In addition to being a class library , It's still a framework , stay vc++ I'll build a new one MFC The engineering of , The development environment will automatically generate many files for you , And it uses mfcxx.dll.xx It's the version , It encapsulates the mfc kernel , So you can't see the original in your code SDK Message loops in programming and so on , because MFC The frame is packaged for you , So you can focus on the logic of your program , Instead of these things that are repeated every time you program , But because it's a common framework , There is no best targeting , Of course, some flexibility and efficiency are lost . however MFC The package is very shallow , So the loss of efficiency is small .
Two 、 Code block
1. Group broadcast sender
The code is as follows ( Example ):
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
int iRet = 0;
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET sock = socket(AF_INET,SOCK_DGRAM,0);
sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("224.2.2.2");
addr.sin_family = AF_INET;
addr.sin_port = htons(55555);
char strSend[1024] = {
0};
static int iIdx = 0;
while(1)
{
sprintf_s(strSend, "udp send group data:%d", iIdx++);
iRet = sendto(sock, strSend, strlen(strSend) + 1, 0, (sockaddr*)&addr, sizeof(sockaddr));
if(iRet <= 0){
printf("send fail:%d", WSAGetLastError());
}else{
printf("send data:%s\n", strSend);
}
Sleep(500);
}
closesocket(sock);
WSACleanup();
return 0;
}
2. Multicast receiver
The code is as follows ( Example ):
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
int iRet = 0;
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
sockaddr_in add, addr;
addr.sin_family = AF_INET;
//addr.sin_addr.S_un.S_addr = INADDR_ANY;
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(55555);
bool bOptval = true;
iRet = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&bOptval, sizeof(bOptval));
if (iRet != 0){
printf("setsockopt fail:%d", WSAGetLastError());
return -1;
}
iRet = bind(sock, (sockaddr*)&addr, sizeof(addr));
if (iRet != 0){
printf("bind fail:%d", WSAGetLastError());
return -1;
}
printf("socket:%d bind success\n", sock);
// Join multicast
ip_mreq multiCast;
//multiCast.imr_interface.S_un.S_addr = INADDR_ANY;
multiCast.imr_interface.S_un.S_addr = inet_addr("127.0.0.1");
multiCast.imr_multiaddr.S_un.S_addr = inet_addr("224.2.2.2");
iRet = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multiCast, sizeof(multiCast));
if (iRet != 0){
printf("setsockopt fail:%d", WSAGetLastError());
return -1;
}
printf("udp group start\n");
int len = sizeof(sockaddr);
char strRecv[1024] = {
0 };
while (true)
{
memset(strRecv, 0, sizeof(strRecv));
iRet = recvfrom(sock, strRecv, sizeof(strRecv)-1, 0, (sockaddr*)&add, &len);
if (iRet <= 0){
printf("recvfrom fail:%d", WSAGetLastError());
return -1;
}
printf("recv data:%s\n", strRecv);
}
closesocket(sock);
WSACleanup();
return 0;
}
summary
- Benchmarking reference Ubuntu Under the udp Multicast
边栏推荐
- day1-jvm+leetcode
- Raspberry pie is of great use. Use the campus network to build a campus local website
- The problem that the user name and password are automatically filled in when Google / Firefox manages the background new account
- 原生js放大镜效果
- How to quickly connect CRM system and ERP system to realize the automatic flow of business processes
- Qt新建工程简介
- Watermelon book / Pumpkin book -- Chapter 1 and 2 Summary
- A small problem in labelme to VOC code
- Problems in SSM project configuration, various dependencies, etc. (for personal use)
- Commands for quickly opening management tools
猜你喜欢
![[activiti] Introduction to activiti](/img/99/e973279d661960853b3af69a7e8ef2.png)
[activiti] Introduction to activiti

jestson安装ibus输入法

Machine learning (zhouzhihua) Chapter 5 notes on neural network learning

Machine learning (Zhou Zhihua) Chapter 4 notes on learning experience of decision tree

Thymeleaf快速入门学习

世界坐标系、相机坐标系和图像坐标系的转换
![OSError: [WinError 127] 找不到指定的程序。Error loading “caffe2_detectron_ops.dll“ or one of its dependencies](/img/1d/4c9924c20f697011f0e9cda6616c12.png)
OSError: [WinError 127] 找不到指定的程序。Error loading “caffe2_detectron_ops.dll“ or one of its dependencies

The problem that the user name and password are automatically filled in when Google / Firefox manages the background new account

信号与系统:希尔伯特变换

MySql与Qt连接、将数据输出到QT的窗口tableWidget详细过程。
随机推荐
原生js放大镜效果
Machine learning (zhouzhihua) Chapter 2 model selection and evaluation notes learning experience
"Statistical learning methods (2nd Edition)" Li Hang Chapter 13 introduction to unsupervised learning mind map notes
字符串方法以及实例
js星星打分效果
对ArrayList<ArrayList<Double>>排序
Statistical learning methods (2nd Edition) Li Hang Chapter 22 summary of unsupervised learning methods mind mapping notes
JVM system learning
Native JS magnifying glass effect
A small problem in labelme to VOC code
jupyter notebook一直自动重启(The kernel appears to have died. It will restart automatically.)
json.dumps()函数解析
[MYCAT] Introduction to MYCAT
JVM系统学习
day1-jvm+leetcode
Thymeleaf快速入门学习
YOLOv5学习总结(持续更新)
[MYCAT] MYCAT sub database and sub table
《统计学习方法(第2版)》李航 第15章 奇异值分解 SVD 思维导图笔记 及 课后习题答案(步骤详细)SVD 矩阵奇异值 十五章
Chapter 5 neural network