zoukankan      html  css  js  c++  java
  • winsock 收发广播包

    ☛广播包的概念

    广播包通常为了例如以下两个原因使用:1 一个应用程序希望在本地网络中找到一个资源,而应用程序对于该资源的地址又没有不论什么先验的知识。
    2 一些重要的功能,比如路由要求把它们的信息发送给全部找到的邻机。
    被广播信息的目的地址取决于这一信息将在何种网络上广播。Internet域中支持一个速记地址用于广播-INADDR_BROADCAST。因为使
    用广播曾经必须捆绑一个数据报套接口,所以全部收到的广播消息都带有发送者的地址和port。

    广播通信是无连接的通信,通信前不须要建立连接。不须要listen和accept,但须要绑定一个socket用来接收广播。

    ☛广播包的发送
    创建socket
    设置socket,比如设置超时、同意广播等
    绑定socket。在使用广播前必须绑定一个socket。这一步可有可无,假设没有,系统自己主动绑定到一个未用port。
    发送广播。广播的port号要和接收方绑定的port号一致

    使用UDP协议发送、接收广播包的过程。

    假如我们要向192.168.0.X,子网掩码为:255.255.255.0的子网中发送广播包。

    其过程例如以下:

    1.       初始化Winsock库。

    2.       创建SOCK_DIRAM类型的Socket。

    3.       设置Socket的属性同意其广播。

    4.       发送数据包到192.168.0.255

    5.       接收自己广播的广播包。

    6.       关闭Socket

    7.       释放网络库。

    注意事项例如以下:

    1.       接收方一定要知道广播方的口号,然后绑定此port号才干正确接收。

    2.       接收方的Socket不须要设置成广播属性。

    3.       绑定的IP不能够使用“127.0.0.1”,能够使用真实IP地址或者INADDR_ANY。否则接收失败。


    复制代码
    #include "stdafx.h"
    #include <WinSock2.h>
    #include <Windows.h>
    #include <string.h>
    
    #pragma comment(lib, "ws2_32.lib")
    
    void autoCleanup()
    {
        WSACleanup();
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
        WORD wVersionRequested;
        WSADATA wsaData;
        wVersionRequested=MAKEWORD(2,2);
        int ret;
        ret = WSAStartup(wVersionRequested,&wsaData);
    
        int sock = socket(AF_INET, SOCK_DGRAM, 0);
        int bc = 1;
        //同意发送广播消息
        int so_broadcast = TRUE;
        ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&so_broadcast, sizeof(so_broadcast));
        
        sockaddr_in addr;
        addr.sin_family = AF_INET; //使用互联网际协议,即IP协议
        addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); 
        addr.sin_port = htons(2526);
    
        //假设不过发送广播,这一步可有可无。没有绑定也能发送广播
        //ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
    
        struct sockaddr_in b_addr;
        b_addr.sin_family = AF_INET;
        b_addr.sin_addr.S_un.S_addr =htonl(INADDR_BROADCAST);
        b_addr.sin_port = htons(2527);
    
    
        char buff[50] = "Hello, world!";
        while (1)
        {
            ret = sendto(sock, buff, strlen(buff), 0, (struct sockaddr*)&b_addr, sizeof(b_addr));
    
            printf("send... %d
    ", WSAGetLastError());
            Sleep(3000);
        }
    
        closesocket(sock);
        atexit(autoCleanup);
        return 0;
    }
    复制代码

    ☛接收广播包
    接收方一定要知道广播方的port号,然后绑定相同的port号才干正确接收。道理非常easy,假设不绑定到一个port,它不知道到哪里接收数据。

    复制代码
    // send.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <WinSock2.h>
    #include <Windows.h>
    #include <string.h>
    
    #pragma comment(lib, "ws2_32.lib")
    
    void autoCleanup()
    {
        WSACleanup();
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        WORD wVersionRequested;
        WSADATA wsaData;
        wVersionRequested=MAKEWORD(2,2);
        WSAStartup(wVersionRequested,&wsaData);
    
        SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
    
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
        //这个端口要和广播方广播端口一致
        addr.sin_port = htons(2527);
    
        bind(sock, (struct sockaddr *)&addr, sizeof(addr));
    
        struct sockaddr_in from;
        int len = sizeof(from);
        int ret;
        char buff[50];
    
        while (1)
        {
            ret = recvfrom(sock, buff, 49, 0, (struct sockaddr *)&from, &len);
            if (ret > 0)
            {
                buff[ret] = 0;
                printf("%s
    ", buff);
                printf("%s %d
    ", inet_ntoa(from.sin_addr), ntohs(from.sin_port));
            }
        }
    
        closesocket(sock);
        atexit(autoCleanup);
        return 0;
    }
  • 相关阅读:
    蓝桥杯--芯片测试
    蓝桥杯--FJ的字符串
    win8 js 没反应
    winform win8 js checkbox互动
    win8 GridView
    wcf服务引用无实体类对象
    arcgis 接口转换与.Net框架
    win8 链接
    分页控件
    Oracle 分页查询存储过程
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4212532.html
Copyright © 2011-2022 走看看