返回> 网站首页 

[转载]正确使用UDP广播

yoours2015-06-27 19:27:04 阅读 1737

简介一边听听音乐,一边写写文章。


// 服务器端
#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)
    {
        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)
        {
            return -1;
        }
        printf("Send: %s/n", buff);
        Sleep(500);
    }

    return 0;
}

// 客户端
#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)
    {
        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)
    {
        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)
        {
            return -1;
        }

        buff[nSendSize] = '/0';
        printf("Recv: %s/n", buff);
    }

    return 0;
}

微信小程序扫码登陆

文章评论

1737人参与,0条评论