first steps with enet

Started by
5 comments, last by Sponji 10 years, 2 months ago

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?

Advertisement
Did you compile enet from source? If so, you can put a breakpoint in the call to recvfrom() where it returns and follow the data through the library.
enum Bool { True, False, FileNotFound };

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.

Derp

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.

enet_host_service handles flushing, but you can also call enet_host_flush to actually send your packets.

Derp

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?

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.

Derp

This topic is closed to new replies.

Advertisement