# How to send an int array and struct via ENet?

I've been using the ENet library for networking and so far it's been working ok. I have established a connection between my client and server. On the client side all it does is send all user input to the server. On the server side I have an array int map[24][24] that stores all of the map tiles. I also have a struct for each player. After I get all of the user input, do all the calculations, and update the map, how would I send the map back to the client? Right now the function I've been using supports sending chars. Would I convert my int array and struct objects to char and send them? How should I accomplish this, also should I send the map and player structs in one packet, or send each in a separate packet? This is my first to experimenting with multiplayer so I apologize if this is a simple mistake or stupid question.

This is my function for sending packets:

void send_packet(int channel, char pack[11]) {
ENetPacket *packet = enet_packet_create(pack, strlen (pack) + 1, ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(peer, channel, packet);
enet_host_flush(host);
}

• Just pack into the buffer the number of elements 24*24 and the data itself, you can use memcpy for that. Then just do the reverse in the client to read it back to an array, its simple really :) – Grimshaw Jul 11 '12 at 21:16
• Ok, also do I send multiple packets, like one with players info and one with map info, or combine everything into one packet? – rmaes4 Jul 11 '12 at 21:20
• I think the industry way to handle it is to have well defined packets. So no combining major packets. This also contributes to protocol clarity. A packet is usually a packet id, which translates to a well defined action, like ClientConnected, WorldInfo, SpawnNPC. Then there is usually a operation count, which just increments as packets are sent. A tick id, so you know WHEN it was sent by the other peer. Then, there is the actual data of your packet, like the id of the npc and coordinates of the spawn. Hope it helps. – Grimshaw Jul 11 '12 at 23:26
• Thanks so much that really clears things up! I found a bunch of networking tutorials that explain how to compensate for lag or just how to send sockets. I'm shocked at the lack of tutorials that just explain how to structure a networked game and what to put in packets, how to pack them etc. I have a good grasp of the syntax I'm simply trying to learn the logic and methodology. – rmaes4 Jul 12 '12 at 2:50
• Glad i can help :) – Grimshaw Jul 12 '12 at 11:32

As a result of my comments to the question, i am pasting them here as an answer.

Just pack into the buffer the number of elements 24*24 and the data itself, you can use memcpy for that. Then just do the reverse in the client to read it back to an array

I think the industry way to handle it is to have well defined packets. So no combining major packets. This also contributes to protocol clarity. A packet is usually a packet id, which translates to a well defined action, like ClientConnected, WorldInfo, SpawnNPC. Then there is usually a operation count, which just increments as packets are sent. A tick id, so you know WHEN it was sent by the other peer. Then, there is the actual data of your packet, like the id of the npc and coordinates of the spawn.

This of course excludes any compression and encryption you may want to implement, which are perfectly possible with these methods.

For a pretty good reference on how to pack and unpack data, check the SFML 2 source code, on the sf::Packet class. Its the best i can think of.

• This is great but doesn't really help me with ENet. I wish ENet had better documentation. Thanks though. – rmaes4 Jul 13 '12 at 1:26
• It does help, I used them both together before. Make your packet with sf::Packet, then assign its raw data to enet packet. Works for sure :) – Grimshaw Jul 13 '12 at 3:21
• Well I'm using allegro and enet right now and would like to avoid using SFML. I've been trying some more basically I'm trying to simply figure out how to send my map as an int array from the server to the client and then have the client render the map. I know how to do the rendering I just simply cannot send the array from the server to the client. I'm getting really frustrated as I cannot find any good resources on the web. I really need to see some source code. – rmaes4 Jul 13 '12 at 3:45
• Packet.cpp from SFML2 source contains the code your targetting, you dont need to use sfml to borrow the idea, thats what ive been trying to say.. ;) – Grimshaw Jul 13 '12 at 11:30
• Wow! I will definitely do that. Thanks so much, sometimes I can be really stupid. – rmaes4 Jul 13 '12 at 18:29