• Advertisement
Sign in to follow this  

[ENET] get a connection

This topic is 3663 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

Hi everybody! I'm trying to get a connection with enet between a server and a client. Everything seems to be initialized correct, but I don't get a connectevent... Ok, here is the Code:
int main()
{
    short i;
    ENetAddress address;
    ENetHost * server;
    ENetHost * client;
    ENetEvent event;
    ENetPeer *peer;
    if (enet_initialize() == -1) {printf("Error beim initialisieren!\n");}
    else {printf("Initialisierung erfolgreich!\n");}
    scanf("%ld",&i);
    switch(i)
    {
             case 1: //Server init
                  address.host = ENET_HOST_ANY;
                  address.port = 1234;
                  server = enet_host_create (& address,32,0,0);
                  if (server == NULL)
                  {
                             fprintf (stderr, 
                             "An error occurred while trying to create an ENet server host.\n");
                             exit (EXIT_FAILURE);
                  }
                  else
                  {
                      printf("Server initialisiert!");
                  }
                  
                  while (enet_host_service (server, & event, 1000) > 0)
                  {
                        switch (event.type)
                        {
                               case ENET_EVENT_TYPE_CONNECT:
                               printf ("A new client connected from %x:%u.\n", 
                                      event.peer -> address.host,
                                      event.peer -> address.port);
                               break;
                         }
                  }
             break;
             case 2: //Client init
                  client = enet_host_create (0,32,0,0);
                  if (client == NULL)
                  {
                             fprintf (stderr, 
                             "An error occurred while trying to create an ENet client host.\n");
                             exit (EXIT_FAILURE);
                  }
                  else
                  {printf("Client initialisiert!");}
                  enet_address_set_host (& address, "localhost");
                  address.port = 1234;
                  peer = enet_host_connect (client, & address, 2);    
                  if (peer == NULL)
                  {
                           fprintf (stderr, 
                                   "No available peers for initiating an ENet connection.\n");
                           exit (EXIT_FAILURE);
                  }
                  
                  while (enet_host_service (client, & event, 1000) > 0)
                  {
                        switch (event.type)
                        {
                               case ENET_EVENT_TYPE_CONNECT:
                               printf ("A new client connected from %x:%u.\n", 
                                      event.peer -> address.host,
                                      event.peer -> address.port);
                               break;
                         }
                  }
             break;
    }
    getch();
}
This should init a server if you enter 1 and should init a client if you enter 2. But the problem is, that I can't get a connection. What am I doing wrong? [EDIT] If somebody can give me the source code of an working example, I would be very pleased! [EDIT2] OK it works now. The problem was, that enet_host_service has to be called in a loop. The thread can be closed now. Thanks for your help! C_Cheaf [Edited by - C_Cheaf on February 13, 2008 7:47:31 AM]

Share this post


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

  • Advertisement