Jump to content
  • Advertisement
Sign in to follow this  

ENet - no connection event is thrown

This topic is 3927 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, I've compiled ENet today and started using it. But I'm running in some troubles now... I wrote a client-side code according to the tutorial. It says "succesfully connected". Then I have the server-side code, also from the tutorial. The server-side code should say "client connected", which it doesn't... What could be the problem? I've unblocked it with firewall, and I first run the server and then the client, yet it refuses to throw an event. Here is the server code:
if (enet_initialize () != 0)
        printf("An error occurred while initializing ENet.\n");
        return EXIT_FAILURE;
    atexit (enet_deinitialize);

    ENetAddress address;
    ENetHost * server;

    /* Bind the server to the default localhost.     */
    /* A specific host address can be specified by   */
    /* enet_address_set_host (& address, "x.x.x.x"); */

    address.host = ENET_HOST_ANY;
    /* Bind the server to port 1234. */
    address.port = 9999;

    server = enet_host_create (& address /* the address to bind the server host to */,
                                 32      /* allow up to 32 clients and/or outgoing connections */,
                                  0      /* assume any amount of incoming bandwidth */,
                                  0      /* assume any amount of outgoing bandwidth */);
    if (server == NULL)
                 "An error occurred while trying to create an ENet server host.\n");
        exit (EXIT_FAILURE);

    ENetEvent event;
    while (1)
        while (enet_host_service (server, & event, 0) > 0)
            printf ("a\n");
            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);

                /* Store any relevant client information here. */
                event.peer -> data = (void *) "Client information";


            case ENET_EVENT_TYPE_RECEIVE:
                printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
                        event.packet -> dataLength,
                        event.packet -> data,
                        event.peer -> data,

                /* Clean up the packet now that we're done using it. */
                enet_packet_destroy (event.packet);


                printf ("%s disconected.\n", event.peer -> data);

                /* Reset the peer's client information. */

                event.peer -> data = NULL;

And the client code:
if (enet_initialize())
        error("could not initialise network", true);

    ENetHost *client = enet_host_create(NULL, 1, 57600/8, 14400/8);
    if (client == NULL)
        error("could not create client host", true);

    ENetAddress address;
    ENetEvent event;
    ENetPeer *peer;

    /* Connect to some.server.net:1234. */
    enet_address_set_host (& address, "");
    address.port = 9999;

    /* Initiate the connection, allocating the two channels 0 and 1. */
    peer = enet_host_connect (client, & address, 2);

    if (peer == NULL)
       fprintf (stderr,
                "No available peers for initiating an ENet connection.\n");
       exit (EXIT_FAILURE);

    /* Wait up to 5 seconds for the connection attempt to succeed. */
    if (enet_host_service (client, & event, 1000) > 0 &&
        event.type == ENET_EVENT_TYPE_CONNECT)
        puts ("Connection to some.server.net:1234 succeeded.");
        /* Either the 5 seconds are up or a disconnect event was */
        /* received. Reset the peer in the event the 5 seconds   */
        /* had run out without any significant event.            */
        enet_peer_reset (peer);

        puts ("Connection to some.server.net:1234 failed.");

PS: yes, the code is a mess, but I will clean it once its working. Thank you in advance! Decrius

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!