• Advertisement
Sign in to follow this  

first steps with enet

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

I decided to invest some time on enet (as it seems to bee the best documented) and networks. I took the tutorial as base to make my own simple client/server test:

This is the server main code:

address.host = ENET_HOST_ANY;
	/* Bind the server to port 1234. */
	address.port = 3234;
	server = enet_host_create (& address /* the address to bind the server host to */,
	32 /* allow up to 32 clients and/or outgoing connections */,
	2 /* allow up to 2 channels to be used, 0 and 1 */,
	0 /* assume any amount of incoming bandwidth */,
	0 /* assume any amount of outgoing bandwidth */);
	if (server == NULL) {
		cout << "An error occurred while trying to create an ENet server host.\n";
		exit (EXIT_FAILURE);
	}

	ENetEvent event;
	bool stop = false;
	cout << "Starting server"<<endl;
	while (!stop){
		enet_host_service (server, & event, 0);
		if (event.type ==ENET_EVENT_TYPE_CONNECT) {
				cout <<"A new client connected from "<<	event.peer->address.host<<":" <<event.peer->address.port<<endl;
				/* Store any relevant client information here. */
				//event.peer -> data = "Client information";
		} else if (event.type ==ENET_EVENT_TYPE_RECEIVE) {
				cout << "A packet was received"<<endl;				
				/* Clean up the packet now that we're done using it. */
				enet_packet_destroy (event.packet);
		} else if (event.type ==ENET_EVENT_TYPE_DISCONNECT) {
				cout << " disconected " <<event.peer -> data<<endl;
				/* Reset the peer's client information. */
				event.peer -> data = NULL;
				stop = false;
		} //if
	} //while

And this is the relevand client code:

ENetHost * client;
	client = enet_host_create (NULL /* create a client host */,
	1 /* only allow 1 outgoing connection */,
	2 /* allow up 2 channels to be used, 0 and 1 */,
	57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
	14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
	if (client == NULL)	{
		cout<< "An error occurred while trying to create an ENet client host.\n";
		exit (EXIT_FAILURE);
	}

	ENetAddress address;
	ENetEvent event;
	ENetPeer *peer;
	/* Connect to some.server.net:1234. */
	enet_address_set_host (& address, "localhost");
	address.port = 3234;
	/* Initiate the connection, allocating the two channels 0 and 1. */
	peer = enet_host_connect (client, & address, 2, 0);
	if (peer == NULL)	{
		cout<< "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){
		cout <<"Connection to localhost:3234 succeeded.";
		/* Create a reliable packet of size 7 containing "packet\0" */
		ENetPacket * packet = enet_packet_create ("ok",	strlen ("ok") + 1, ENET_PACKET_FLAG_RELIABLE);
		enet_peer_send (peer, 0, packet);
		enet_peer_disconnect (peer, 0);
	}	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);
		cout <<"Connection to some.server.net:3234 failed.";
	}

The problem is that the client reports that it connected successfully, but the server does not notify of any connection or packet received. I have checked the code several times and cant find any logical error, so probably Im missing some aspect not documented on the tutorials. Can somebody give me an idea?

Share this post


Link to post
Share on other sites
Advertisement

Try making a similar loop for client and make sure you call enet_host_service again after sending packets/disconnecting, I think your client just exits without sending anything. Also, I'd suggest to use a while-loop for enet_host_service so it handles all events at once.

Share this post


Link to post
Share on other sites

Try to not disconnect immedately after sending the packet. You might have to do a flush or something tricky if you want something like that.

Share this post


Link to post
Share on other sites

Indeed, adding a flush solved the problem. So, if the server is attending several clients, do I have to call flush everytime it replies a client packet? That wouldnt kill perfomance or something?

Share this post


Link to post
Share on other sites

You don't have to flush all the time, enet_host_service will take care of all that sending and receiving. Just handle the network events with enet_host_service from your main loop.

Share this post


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

  • Advertisement