您当前的位置: 首页 > 

qianbo_insist

暂无认证

  • 0浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

关于socket组播和ssdp(二)

qianbo_insist 发布时间:2021-06-28 13:05:26 ,浏览量:0

关于socket组播和ssdp(一)

1、说明

在制作的过程中,实际上ssdp发现协议特别简单,只是加入组播后,发送搜索的字符串,然后再在单播上接收,如果是发送,则要发送到多播地址,而且,发送的字符串不能出错,这里说明作者的一个错误,开始时,“MAN: “ssdp:discover”\r\n”,一直写成了"MAN: ssdp:discover\r\n",所以在单播上没有收到数据,值得注意!

2、show me the code,以下用boost库来做组播的接收和发送

socket.bind( udp::endpoint(boost::asio::ip::address_v4::any(), receiver ? port /* same as multicast port / : 6200 / any */)); 以上这句话比较重要,在使用发送的时候,使用组播端口,接收的时候,使用本地的一个端口,切记!

#define _CRT_SECURE_NO_WARNINGS
#include 
// 3rd party includes.
#include 
#include 
#include 


static const char msearchmsgfmt[] = "M-SEARCH * HTTP/1.1\r\n"
"Connection: close"
"HOST: 239.255.255.250:1900\r\n"
"ST: %s\r\n"
"MAN: \"ssdp:discover\"\r\n"
"USER-AGENT: qbupnp 1.0"
"MX: 3\r\n\r\n";



void read(boost::asio::ip::udp::socket& socket)
    {
        boost::asio::ip::udp::endpoint sender;
        std::vector buffer;
        std::size_t bytes_readable = 0;
        for (;;)
        {
            // Poll until data is available.
            while (!bytes_readable)
            {
                // Issue command to socket to get number of bytes readable.
                boost::asio::socket_base::bytes_readable num_of_bytes_readable(true);
                socket.io_control(num_of_bytes_readable);

                // Get the value from the command.
                bytes_readable = num_of_bytes_readable.get();

                // If there is no data available, then sleep.
                if (!bytes_readable)
                {
                    boost::this_thread::sleep(boost::posix_time::seconds(1));
                }
            }

            // Resize the buffer to store all available data.
            buffer.resize(bytes_readable);

            // Read available data.
            socket.receive_from(
                boost::asio::buffer(buffer, bytes_readable),
                sender);

            // Extract data from the buffer.
            std::string message(buffer.begin(), buffer.end());

            // Output data.
            std::cout             
关注
打赏
1663161521
查看更多评论
0.0383s