windows下c++upd收发广播信息示例代码
时间: 2024-03-27 22:36:53 浏览: 81
以下是Windows下C++实现UDP收发广播信息的示例代码:
发送端代码:
```
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
// 初始化Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
cout << "WSAStartup failed: " << iResult << endl;
return 1;
}
// 创建socket
SOCKET sendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sendSocket == INVALID_SOCKET) {
cout << "Failed to create socket: " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 设置广播属性
BOOL bOptVal = TRUE;
iResult = setsockopt(sendSocket, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, sizeof(BOOL));
if (iResult == SOCKET_ERROR) {
cout << "setsockopt failed: " << WSAGetLastError() << endl;
closesocket(sendSocket);
WSACleanup();
return 1;
}
// 设置目标地址
SOCKADDR_IN destAddr;
destAddr.sin_family = AF_INET;
destAddr.sin_port = htons(12345);
destAddr.sin_addr.s_addr = INADDR_BROADCAST;
// 发送数据
char sendBuf[] = "Hello, this is a broadcast message";
iResult = sendto(sendSocket, sendBuf, strlen(sendBuf), 0, (SOCKADDR*)&destAddr, sizeof(destAddr));
if (iResult == SOCKET_ERROR) {
cout << "sendto failed: " << WSAGetLastError() << endl;
closesocket(sendSocket);
WSACleanup();
return 1;
}
// 关闭socket
closesocket(sendSocket);
WSACleanup();
return 0;
}
```
接收端代码:
```
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
// 初始化Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
cout << "WSAStartup failed: " << iResult << endl;
return 1;
}
// 创建socket
SOCKET recvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (recvSocket == INVALID_SOCKET) {
cout << "Failed to create socket: " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 设置广播属性
BOOL bOptVal = TRUE;
iResult = setsockopt(recvSocket, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, sizeof(BOOL));
if (iResult == SOCKET_ERROR) {
cout << "setsockopt failed: " << WSAGetLastError() << endl;
closesocket(recvSocket);
WSACleanup();
return 1;
}
// 设置本地地址
SOCKADDR_IN localAddr;
localAddr.sin_family = AF_INET;
localAddr.sin_port = htons(12345);
localAddr.sin_addr.s_addr = INADDR_ANY;
// 绑定socket
iResult = bind(recvSocket, (SOCKADDR*)&localAddr, sizeof(localAddr));
if (iResult == SOCKET_ERROR) {
cout << "Failed to bind socket: " << WSAGetLastError() << endl;
closesocket(recvSocket);
WSACleanup();
return 1;
}
// 接收数据
char recvBuf[1024] = { 0 };
SOCKADDR_IN fromAddr;
int fromLen = sizeof(fromAddr);
iResult = recvfrom(recvSocket, recvBuf, sizeof(recvBuf), 0, (SOCKADDR*)&fromAddr, &fromLen);
if (iResult == SOCKET_ERROR) {
cout << "recvfrom failed: " << WSAGetLastError() << endl;
closesocket(recvSocket);
WSACleanup();
return 1;
}
// 显示收到的数据
cout << "Received message: " << recvBuf << endl;
// 关闭socket
closesocket(recvSocket);
WSACleanup();
return 0;
}
```
这个示例代码实现了在本地网络中发送广播消息,并在另一台机器上接收。要注意的是,如果有多台机器在同一个局域网下运行这个示例代码,那么它们都会收到这个广播消息。
阅读全文