正确使用UDP广播(C++代码)

相比TCP,UDP的优点是传输速度快且能对整个网络广播数据。但我以前在使用UDP整个网络广播数据的用法上,一直没能好好使用。这段时间,正好需要使用UDP的这个功能,因此经过摸索,得出了一种使用方法如下:(不一定是最好的,但能实现我的功能)

// 服务器端
// Server.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <WinSock2.h>
#include <stdio.h>
#include <iostream>
using namespace std;
 
#pragma comment(lib, "ws2_32.lib")
 
const int MAX_BUF_LEN = 255;
 
int _tmain(int argc, _TCHAR* argv[])
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
 
	// 启动socket api
	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
	SOCKET connect_socket;
	connect_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if(INVALID_SOCKET == connect_socket)
	{
		err = WSAGetLastError();
		printf("/"socket/" error! error code is %d/n", err);
		return -1;
	}
 
	SOCKADDR_IN sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(3779);
	sin.sin_addr.s_addr = INADDR_BROADCAST;
 
	bool bOpt = true;
	//设置该套接字为广播类型
	setsockopt(connect_socket, SOL_SOCKET, SO_BROADCAST, (char*)&bOpt, sizeof(bOpt));
 
	int nAddrLen = sizeof(SOCKADDR);
 
	char buff[MAX_BUF_LEN] = "";
	int nLoop = 0;
	while(1)
	{
		nLoop++;
		sprintf(buff, "%8d", nLoop);
 
		// 发送数据
		int nSendSize = sendto(connect_socket, buff, strlen(buff), 0, (SOCKADDR*)&sin, nAddrLen);
		if(SOCKET_ERROR == nSendSize)
		{
			err = WSAGetLastError();
			printf("/"sendto/" error!, error code is %d/n", err);
			return -1;
		}
		printf("Send: %s/n", buff);
		Sleep(500);
	}
 
	return 0;
}
 
// 客户端
// Client.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <WinSock2.h>
#include <stdio.h>
 
#pragma comment(lib, "ws2_32.lib")
 
const int MAX_BUF_LEN = 255;
 
int _tmain(int argc, _TCHAR* argv[])
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
 
	// 启动socket api
	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
	SOCKET connect_socket;
	connect_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if(INVALID_SOCKET == connect_socket)
	{
		err = WSAGetLastError();
		printf("/"socket/" error! error code is %d/n", err);
		return -1;
	}
 
	// 用来绑定套接字
	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("/"bind/" error! error code is %d/n", err);
		return -1;
	}
 
	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("/"recvfrom/" error! error code is %d/n", err);
			return -1;
		}
		buff[nSendSize] = '/0';
		printf("Recv: %s/n", buff);
	}
 
	return 0;
}

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

发表评论

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