Sign in to follow this  

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

This topic is 1831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

 

,

Share this post


Link to post
Share on other sites

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

}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this