• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
rogerdv

first steps with enet

6 posts in this topic

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?

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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.

0

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.

1

Share this post


Link to post
Share on other sites

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

2

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?

0

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.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0