How can enet listening more than 1 port at same time?

Started by
1 comment, last by BeerNutts 11 years, 3 months ago

in my project requires to listening about 3 port at same time for difference busniess , but for enet demo code:

once i established a server by


ENetHost* create server(int port){
ENetAddress* addr;
addr->host=.....
addr->port=port;

server=p2udp_host_create(&serv_addr,MAX_CLIENT_LINKS,MAX_CHANNEL_SET,0,0);

????

return server;
}

than i call this function 3 times so i got about 3 server instance with 3 different ports, but only 1 port can handle events which was first made.

so the question is how can i handle the message events for all of 3 ports because event handling procedure is in located a while loop just like:


service_event_handle(ENetHost* server)
{
  ENETEVENET event;
  while(1){
    switch(.....){
    }
 }

}

when i pass the params server1 create by , it just traped in loop without ending conditions

i wonder how can i jump the loop do handle evnets for another 2 port

,

Advertisement

You need to re-structure your code a bit? You need to store all your servers in a container, and just loop over all of them each time instead of letting any one server create the infinite loop.


while (!servers.empty()) {

  for ( std::vector<ENetHost *>::iterator server = servers.begin(); server != servers.end(); ++server) {

    ENETEVENT event;

    if(enet_host_service (client, & event, 3000) > 0)  {
      switch (event.type)
    } else {

      // flag server for disconnect

    }

  }

  // loop again, removing dead servers

}

For generic sockets, you create the sockets you need to listen on and bind to the ports you need, then add those sockets to a file descriptor and call select() with that FD. It will handle everything for you.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement