[ENet-discuss] problem: no client connection events from server.

Ryan Gumbs project.phelius at gmail.com
Sat Dec 10 08:57:00 PST 2005


Helllo. I'm new to ENet and I'm having trouble getting the tutorial
source to work.
here is my simple code. I've compiled enet and my source on Debian Linux 2.6.12
My problem is that the client prints messages saying it connected,
however the server never receives the connection event from client.
Oddly enough, it does receive/print events for disconnects.
Maybe that's just because it lost contact with the client.

This is pretty much the tutorial source code verbatim.
Any help would be much appreciated.
thx,
Phelius.


to start as a server:
> ./try 1
to start as a client
> ./try


try.c ===========
#include <stdio.h>
#include "enet/enet.h"

    ENetHost * server=0;
    ENetHost * client=0;

int start_server () {
    ENetAddress address;

    /* 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 = 12345;

    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)
    {
        fprintf (stderr,
                 "An error occurred while trying to create an ENet
server host.\n");
        exit (EXIT_FAILURE);
    }
printf ("server started\n");
fflush(stdout);
return 0;
}


int start_client() {
ENetAddress address;
ENetEvent event;
ENetPeer *peer;


    client = enet_host_create (NULL /* create a client host */,
                1 /* only allow 1 outgoing connection */,
                57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
                14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);

    if (client == NULL)
    {
        fprintf (stderr,
                 "An error occurred while trying to create an ENet
client host.\n");
        exit (EXIT_FAILURE);
    }



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

    /* 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, 5000) > 0 &&
        event.type == ENET_EVENT_TYPE_CONNECT)
    {
        puts ("Connection to some.server.net:1234 succeeded.\n");
    }
    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.\n");
	exit(EXIT_FAILURE);
    }
fflush(stdout);
return 0;
}



int do_server_events() {
    ENetEvent event;

    /* Wait up to 1000 milliseconds for an event. */
    while (enet_host_service (server, & event, 1000) > 0)
    {
        switch (event.type)
        {
        case ENET_EVENT_TYPE_CONNECT:
	printf ("A new connection\n");
	fflush(stdout);
//            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 = "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);
		fflush(stdout);
            //* 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);
		fflush(stdout);
            /* Reset the peer's client information. */

            event.peer -> data = NULL;
        }
    }

}



int main(int argc, char **argv) {

   if (enet_initialize () != 0)
    {
        fprintf (stderr, "An error occurred while initializing ENet.\n");
        return EXIT_FAILURE;
    }
    atexit (enet_deinitialize);

if (argc==2) { // server
	start_server();
	while (1) {
		do_server_events();
		}
	}
else {
	start_client();
	while (1) {
		}
	}
}



More information about the ENet-discuss mailing list