C++ 网络编程之正确使用UDP广播及多播

    大多数的事倍功半源于自以为是事半功倍的一念之差。查了两天UDP传输代码,终于从一个博客的一行小字发现了问题的所在。之前为了方便直接运用linux下写好的客户端代码稍作修改之后去跟linux的服务器对接,造成有的机器收的到数据包有的机器却收不到数据包,或是有时收的到数据包有时收不到数据包的状况。可见不同操作系统之间的移植哪怕是同种语言也并不一定是表面看的那么简单,再者生搬硬套的做法往往得不偿失。下面贴出在网上找到的关于正确使用UDP广播的代码,由于多播和广播相差无几,是故以后会把项目的多播代码贴上,以补全题目内容。

    代码出自这里,需要看原文的可点击链接:点击打开链接

// 服务器端
// Server.cpp : Defines the entry point for the console application.
//

#include <WinSock2.h>
#include <stdio.h>
#include <iostream>
using namespace std;

#pragma comment(lib, "ws2_32.lib")
const int MAX_BUF_LEN = 10;

void main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;

	// 启动socket api
	wVersionRequested = MAKEWORD(2, 2);
	err = WSAStartup(wVersionRequested, &wsaData);
	if (err != 0)
	{
		return;
	}

	if (LOBYTE(wsaData.wVersion) != 2 ||
		HIBYTE(wsaData.wVersion) != 2)
	{
		WSACleanup();
		return;
	}

	// 创建socket
	SOCKET connect_socket;
	connect_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (INVALID_SOCKET == connect_socket)
	{
		err = WSAGetLastError();
		printf("error! error code is %d/n", err);
		return;
	}

	SOCKADDR_IN sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(3779);
	//sin.sin_addr.s_addr = INADDR_BROADCAST;
	sin.sin_addr.s_addr = inet_addr("255.255.255.255");//设置广播地址,window下用这种形式方可

	bool bOpt = true;
	//设置该套接字为广播类型
	setsockopt(connect_socket, SOL_SOCKET, SO_BROADCAST, (char*)&bOpt, sizeof(bOpt));

	int nAddrLen = sizeof(SOCKADDR);

	char buff[MAX_BUF_LEN] = "";
	while (1)
	{
		sprintf_s(buff, "%s","ss");

		// 发送数据
		int nSendSize = sendto(connect_socket, buff, strlen(buff), 0, (SOCKADDR*)&sin, nAddrLen);
		if (SOCKET_ERROR == nSendSize)
		{
			err = WSAGetLastError();
			printf("error!, error code is %d/n", err);
			return;
		}
		printf("Send: %s/n", buff);
		Sleep(500);
	}
}
 
 

// 客户端
// Client.cpp : Defines the entry point for the console application.
//

#include <WinSock2.h>
#include <stdio.h>

#pragma comment(lib, "ws2_32.lib")
const int MAX_BUF_LEN = 255;

void main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;

	// 启动socket api
	wVersionRequested = MAKEWORD(2, 2);
	err = WSAStartup(wVersionRequested, &wsaData);
	if (err != 0)
	{
		return;
	}

	if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
	{
		WSACleanup();
		return;
	}

	// 创建socket
	SOCKET connect_socket;
	connect_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (INVALID_SOCKET == connect_socket)
	{
		err = WSAGetLastError();
		printf("error! error code is %d/n", err);
		return;
	}

	// 用来绑定套接字
	SOCKADDR_IN sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(3779);
	sin.sin_addr.s_addr = 0;

	// 用来从网络上的广播地址接收数据
	SOCKADDR_IN sin_from;
	sin_from.sin_family = AF_INET;
	sin_from.sin_port = htons(3779);
	sin_from.sin_addr.s_addr = INADDR_BROADCAST;

	//设置该套接字为广播类型,
	bool bOpt = true;
	setsockopt(connect_socket, SOL_SOCKET, SO_BROADCAST, (char*)&bOpt, sizeof(bOpt));

	// 绑定套接字
	err = bind(connect_socket, (SOCKADDR*)&sin, sizeof(SOCKADDR));
	if (SOCKET_ERROR == err)
	{
		err = WSAGetLastError();
		printf("error! error code is %d/n", err);
		return;
	}

	int nAddrLen = sizeof(SOCKADDR);
	char buff[MAX_BUF_LEN] = "";
	int nLoop = 0;
	while (1)
	{
		// 接收数据
		int nSendSize = recvfrom(connect_socket, buff, MAX_BUF_LEN, 0, (SOCKADDR*)&sin_from, &nAddrLen);
		if (SOCKET_ERROR == nSendSize)
		{
			err = WSAGetLastError();
			printf("error! error code is %d/n", err);
			return;
		}
		buff[nSendSize] = '/0';
		printf("Recv: %s/n", buff);
	}
}
 
 

你可能感兴趣的:(C++,UDP广播包收发)