Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
godsonhyl opened this issue Jun 3, 2021 · 1 comment
Open

Comments

@godsonhyl
Copy link

godsonhyl commented Jun 3, 2021

#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
Copy link
Author

godsonhyl commented Jun 3, 2021

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

@godsonhyl godsonhyl changed the title multicast msg can only be consumed by one client? multicast msg can only be received/consumed by one client? Jun 3, 2021
@godsonhyl godsonhyl changed the title multicast msg can only be received/consumed by one client? multicastgroup msg can only be received/consumed by one client? Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant