windows 下 c++ socket udp 广播

接收广播消息端:

#include "stdafx.h"
#include <iostream>
#include <WinSock2.h>
 
#pragma  comment(lib, "ws2_32.lib")
 
const int MAX_BUF_LEN = 255;
 
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
 
	wVersionRequested = MAKEWORD(2, 2);
	err = WSAStartup(wVersionRequested, &wsaData);
	if(err != 0)
	{
		return -1;
	}
	if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
	{
		WSACleanup();
		return -1;
	}
 
	SOCKET clientSock = socket(AF_INET, SOCK_DGRAM, 0);
	if (INVALID_SOCKET == clientSock)
	{
		err = WSAGetLastError();
		printf("socket error:%d\n", err);
		WSACleanup();
		return -1;
	}
	//创建本地地址信息并绑定
	SOCKADDR_IN sin;
	sin.sin_family = AF_INET;
	sin.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	sin.sin_port = htons(7001);
	if (bind(clientSock, (SOCKADDR*)&sin, sizeof(sin)) != 0)
	{
		err = WSAGetLastError();
		printf("bind error:%d\n", err);
		closesocket(clientSock);
		WSACleanup();
		return -1;
	}
 
	SOCKADDR_IN clientAddr;
	int addrLen = sizeof(clientAddr);
	int nSize;
	char recvBuff[1024];
	memset(recvBuff, 0, 1024);
 
	while(1){
		nSize = recvfrom(clientSock, recvBuff, 1024, 0, (SOCKADDR*)&clientAddr,&addrLen);
		if ( nSize == SOCKET_ERROR)
		{
			err = WSAGetLastError();
			printf("recv error:%d\n", err);
			break;
		}
		recvBuff[nSize] = '\0';
		printf("%s\n", recvBuff);
 
	}
 
	closesocket(clientSock);
	WSACleanup();
	return 0;
}

发送广播消息端:

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <WinSock2.h>
 
#pragma comment(lib, "ws2_32.lib")
 
using namespace std;
 
const int MAX_BUF_LEN = 255;
 
int _tmain(int argc, _TCHAR* argv[])
{
	WORD wVersionRequested;
	WSADATA wasData;//指向WinSocket信息结构的指针
	int err;
 
	wVersionRequested = MAKEWORD(2, 2);
	// 初始化winsock库,使用socket的前提
	err = WSAStartup(wVersionRequested, &wasData);
	if (err != 0)
	{
		return -1;
	}
	if (LOBYTE(wasData.wVersion) != 2 || HIBYTE(wasData.wVersion) != 2)
	{
		WSACleanup();
		return -1;
	}
	SOCKET conn_sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (conn_sock == INVALID_SOCKET)
	{
		err = WSAGetLastError();
		printf("socket error:%d\n", err);
		closesocket(conn_sock);
		WSACleanup();
		return -1;
	}
	bool bOpt = true;
	//打开广播选项
	setsockopt(conn_sock, SOL_SOCKET, SO_BROADCAST, (char*)&bOpt, sizeof(bOpt));
 
	// 设置发往的地址
	SOCKADDR_IN saUdpServ;
	memset(&saUdpServ, 0, sizeof(saUdpServ));
	saUdpServ.sin_family = AF_INET;
	saUdpServ.sin_addr.S_un.S_addr = htonl(INADDR_BROADCAST);
	saUdpServ.sin_port = htons(7001);
	int nSize = sizeof(SOCKADDR_IN);	
 
	char buff[MAX_BUF_LEN];
	memset(buff, 0, MAX_BUF_LEN);
	int nLoop = 0;
	char* ip = NULL;
	while(1){
		nLoop++;
		sprintf(buff, "Message %d", nLoop);
		int nSendSize = sendto(conn_sock, buff, strlen(buff), 0, (SOCKADDR*)&saUdpServ, nSize);
		if (nSendSize == SOCKET_ERROR)
		{
			err = WSAGetLastError();
			printf("sendto error:%d\n", err);
			closesocket(conn_sock);
			WSACleanup();
			return -1;
		}
		printf("send: %s\n", buff);
		Sleep(5000);
	}
 
	closesocket(conn_sock);
	WSACleanup();
	return 0;
}

还没有评论,快来抢沙发!

发表评论

  • 😉
  • 😐
  • 😡
  • 😈
  • 🙂
  • 😯
  • 🙁
  • 🙄
  • 😛
  • 😳
  • 😮
  • emoji-mrgree
  • 😆
  • 💡
  • 😀
  • 👿
  • 😥
  • 😎
  • ➡
  • 😕
  • ❓
  • ❗
  • 66 queries in 0.391 seconds