netLink icon indicating copy to clipboard operation
netLink copied to clipboard

multicastgroup msg can only be received/consumed by one client?

Open godsonhyl opened this issue 4 years ago • 1 comments

#include <iostream>
#include <thread>
#include "../include/netLink.h"

int main(int argc, char** argv) {

#ifdef WINVER
	netLink::init();
#endif

	netLink::SocketManager socketManager;

	socketManager.onReceiveMsgPack = [](netLink::SocketManager* manager, std::shared_ptr<netLink::Socket> socket, std::unique_ptr<MsgPack::Element> element) {		
		auto& str = static_cast<MsgPack::String&>(*element.get());
		auto val = atoi(str.getData());
		std::cout << socket->hostRemote << " val:" << val << std::endl;
	};

	std::shared_ptr<netLink::Socket> socket = socketManager.newMsgPackSocket();

	try {
		socket->initAsUdpPeer("*", 3824);
	}
	catch (netLink::Exception exc) {
		std::cout << "Address is already in use" << std::endl;
		return 1;
	}
	
	socket->hostRemote = (socket->getIPVersion() == netLink::Socket::IPv4) ? "224.0.0.100" : "FF02:0001::";
	socket->portRemote = socket->portLocal;

	socket->setMulticastGroup(socket->hostRemote, true);

	std::thread t1([&](){
		while (true){
			socketManager.listen();
		}
	});

	netLink::MsgPackSocket& msgPackSocket = *static_cast<netLink::MsgPackSocket*>(socket.get());

	for (int i = 0; i < 100; ++i){			
		char msg[256] = {0};
		sprintf(msg, "%02d", i);
		msgPackSocket << MsgPack::Factory(msg);
		std::this_thread::sleep_for(std::chrono::seconds(1));
	}

	t1.join();

        return 0;
}

godsonhyl avatar Jun 03 '21 11:06 godsonhyl

  • 多个client加入同一组播组,每个client向组播组发消息,并监听该组播组消息;
  • 但测试下来发现组播内消息只能被一个client接收/消费一次,和预期的不一致;
  • 预期是组播消息能被所有client接收到;
  • 该如何修改这个demo使所有client接收到组播内所有消息?

godsonhyl avatar Jun 03 '21 11:06 godsonhyl