• Advertisement
Sign in to follow this  

ENet - no connection event is thrown

This topic is 3620 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)
    {
        printf(
                 "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";

                break;

            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,
                        event.channelID);

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

                break;

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

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

                event.peer -> data = NULL;
            }
        }
    }

    enet_host_destroy(server);

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, "127.0.0.1");
    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.");
    }
    else
    {
        /* 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.");
    }

enet_host_destroy(client);
    enet_deinitialize();

PS: yes, the code is a mess, but I will clean it once its working. PPS: repost, but I thought this section fits better, if not this post can be deleted. Thank you in advance! Decrius

Share this post


Link to post
Share on other sites
Advertisement
I've done some more research, and found out I have to place the event checking for the server in a loop...done that but still it doesn't say a client has connected...

Anyone with the same problem?

Thanks.

Share this post


Link to post
Share on other sites
I don't use Enet so I can be of limited help here. However, the fact that the client is connecting successfully makes me believe your server is correctly setting up the socket, and therefore the problem is in one of these places:
- you have a different server listening on that port already (should give you an error when trying to start another one)
- you have redirected stdout somewhere such that the console output is getting lost
- something in your Enet server loop is not right.

Does your code print the lines of 'a' that you inserted in the loop? If not, you are passing something wrong to enet_host_service (or calling the wrong function), imo.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bob Janova
I don't use Enet so I can be of limited help here. However, the fact that the client is connecting successfully makes me believe your server is correctly setting up the socket, and therefore the problem is in one of these places:
- you have a different server listening on that port already (should give you an error when trying to start another one)
- you have redirected stdout somewhere such that the console output is getting lost
- something in your Enet server loop is not right.

Does your code print the lines of 'a' that you inserted in the loop? If not, you are passing something wrong to enet_host_service (or calling the wrong function), imo.


Thanks...but it didn't solve it.

Only when I send a packet to the server it says it has a connected client =/, weird.

Share this post


Link to post
Share on other sites
I'm having the exact same problem. If you figure it out make sure to post here. I will do the same

Share this post


Link to post
Share on other sites
As far as I know, Enet bufferers protocol messages to reduce traffic. This means it may not send the connection event until you send the first packet. If you call enet_host_flush in the client after is accepts the connection it will force enet to send the connection message to the server.

I hope that helps!

Share this post


Link to post
Share on other sites
Quote:
Original post by sphen_lee
As far as I know, Enet bufferers protocol messages to reduce traffic. This means it may not send the connection event until you send the first packet. If you call enet_host_flush in the client after is accepts the connection it will force enet to send the connection message to the server.

I hope that helps!


Thanks for the reply. I tried this, and it did not work :(

Share this post


Link to post
Share on other sites
Quote:
Original post by sphen_lee
As far as I know, Enet bufferers protocol messages to reduce traffic. This means it may not send the connection event until you send the first packet. If you call enet_host_flush in the client after is accepts the connection it will force enet to send the connection message to the server.

I hope that helps!


Yes that helps for me :).

That also explains why the server received the connection only when you send a package (and afterwards flushed the host).

@c_olin: this is the code that works for me:

#include <iostream>

#include "enet/enet.h"
#include "enet/list.h"
#include "enet/protocol.h"
#include "enet/time.h"
#include "enet/types.h"
#include "enet/utility.h"
#include "enet/win32.h"
//#include "enet/unix.h"

using namespace std;

int main()
{
if (enet_initialize())
{
cout << "An error occurred while initializing ENet.\n";
return 1;
}
atexit(enet_deinitialize);

char input;
cout << "Server (s) or Client (c)?\n>";
cin >> input;

ENetAddress address;
ENetEvent event;

if (input == 's')
{
address.host = ENET_HOST_ANY;
address.port = 5000;

ENetHost *server = enet_host_create(&address, 32, 0, 0);
if (server == NULL)
{
cout << "An error occurred while trying to create an ENet server host.\n";
return 1;
}

while (1)
{
while (enet_host_service(server, & event, 10) > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_CONNECT:
{
//cout << "Connected: " << event.peer->address.host << ":" << event.peer->address.port << '\n';

printf ("A new client connected from %x:%u.\n",
event.peer -> address.host,
event.peer -> address.port);

event.peer->data = (void *) "Client information";
break;
}
case ENET_EVENT_TYPE_RECEIVE:
{
cout << event.packet->dataLength << ": " << event.packet->data << '\n';
enet_packet_destroy(event.packet);
break;
}
case ENET_EVENT_TYPE_DISCONNECT:
{
cout << "Disconected.\n";
event.peer->data = NULL;
break;
}
default:
{}
}
}
}
enet_host_destroy(server);
}
else if (input == 'c')
{
ENetHost *client = enet_host_create(NULL, 1, 57600 / 8, 14400 / 8);
if (client == NULL)
{
cout << "An error occurred while trying to create an ENet client host.\n";
return 1;
}

enet_address_set_host(&address, "localhost");
address.port = 5000;

ENetPeer *peer = enet_host_connect(client, &address, 2);
if (peer == NULL)
{
printf("No available peers for initiating an ENet connection.\n");
return 1;
}

if (enet_host_service(client, & event, 100) > 0 && event.type == ENET_EVENT_TYPE_CONNECT)
{
cout << "Connection succeeded.";

enet_host_flush(client);
}
else
{
enet_peer_reset (peer);

cout << "Connection to some.server.net:1234 failed.";
}

enet_host_destroy(client);
}

system("PAUSE");
return 0;
}



I hope this helps you.

One problem in:

printf ("A new client connected from %x:%u.\n",
event.peer -> address.host,
event.peer -> address.port);

The event.peer -> address.host shows very weird format of the IP address...is there someway I can make it look like an ordinary IP address? With the dots?

Share this post


Link to post
Share on other sites
@Decrius: You helped me with enet, so thanks.

About event.peer -> address.host it's uint32, 32-bit integer, and an ordinary IP address consists of four chars showed as int.
So to get it into char array i used this:


char IP[4];

IP[0] = event.peer -> address.host&0xff; event.peer -> address.host>>=8;
IP[1] = event.peer -> address.host&0xff; event.peer -> address.host>>=8;
IP[2] = event.peer -> address.host&0xff; event.peer -> address.host>>=8;
IP[3] = event.peer -> address.host&0xff;

cout << (int)IP[0] << "." << (int)IP[1] << "." << (int)IP[2] << "." << (int)IP[3] << endl;




Hope that helps and sorry for my English.

Share this post


Link to post
Share on other sites
Quote:
Original post by m4tth3w
@Decrius: You helped me with enet, so thanks.

About event.peer -> address.host it's uint32, 32-bit integer, and an ordinary IP address consists of four chars showed as int.
So to get it into char array i used this:

*** Source Snippet Removed ***

Hope that helps and sorry for my English.


Thanks with that :), works perfectly.

Share this post


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

  • Advertisement