From 818a78efe3d7eeaa8362d3d7b078e32a240bd551 Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Sat, 4 May 2013 21:28:51 -0700 Subject: [PATCH] enet NAT punchthrough test improvement, seems to work fine for my test setup --- test/enet/client/main.cpp | 38 +++++++++++++++++++++----------------- test/enet/server/main.cpp | 6 ++++-- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/test/enet/client/main.cpp b/test/enet/client/main.cpp index edc273da0..a351c6d2d 100644 --- a/test/enet/client/main.cpp +++ b/test/enet/client/main.cpp @@ -52,7 +52,7 @@ int main(int argc, char* argv[]) ENetPeer* natpeer=0; /* Connect to some.server.net:1234. */ - enet_address_set_host (& dedicatedserveraddress, "localhost"); + enet_address_set_host (& dedicatedserveraddress, "bulletphysics.org");//localhost"); dedicatedserveraddress.port = 1234; /* Initiate the connection, allocating the two channels 0 and 1. */ dedicatedpeer = enet_host_connect (client, & dedicatedserveraddress, 2, 0); @@ -65,29 +65,33 @@ int main(int argc, char* argv[]) if (enet_host_service (client, & event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { - puts ("Connection to some.server.net:1234 succeeded."); - -#if 0 - /* Create a reliable packet of size 7 containing "packet\0" */ - ENetPacket * packet = enet_packet_create ("packet", - strlen ("packet") + 1, - ENET_PACKET_FLAG_RELIABLE); - /* Extend the packet so and append the string "foo", so it now */ - /* contains "packetfoo\0" */ - enet_packet_resize (packet, strlen ("packetfoo") + 1); - strcpy ((char*)& packet -> data [strlen ("packet")], "foo"); - /* Send the packet to the peer over channel id 0. */ - /* One could also broadcast the packet by */ - /* enet_host_broadcast (host, 0, packet); */ - enet_peer_send (dedicatedpeer, 0, packet); -#endif + char servername[1024]; + enet_address_get_host(&dedicatedserveraddress,servername, 1024); + char serverinfo[1024]; + sprintf(serverinfo,"Connection to %s:%d succeeded", servername,dedicatedserveraddress.port); + puts (serverinfo); /////.... /* Wait up to 1000 milliseconds for an event. */ while (enet_host_service (client, & event, 1000000000) > 0) { + if (natpeer) + { + /* Create a reliable packet of size 7 containing "packet\0" */ + ENetPacket * packet = enet_packet_create ("packet", + strlen ("packet") + 1, + ENET_PACKET_FLAG_RELIABLE); + /* Extend the packet so and append the string "foo", so it now */ + /* contains "packetfoo\0" */ + enet_packet_resize (packet, strlen ("packetfoo") + 1); + strcpy ((char*)& packet -> data [strlen ("packet")], "foo"); + /* Send the packet to the peer over channel id 0. */ + /* One could also broadcast the packet by */ + /* enet_host_broadcast (host, 0, packet); */ + enet_peer_send (natpeer, 0, packet); + } switch (event.type) { case ENET_EVENT_TYPE_CONNECT: diff --git a/test/enet/server/main.cpp b/test/enet/server/main.cpp index 19e2fa2b6..31ea9cc46 100644 --- a/test/enet/server/main.cpp +++ b/test/enet/server/main.cpp @@ -47,8 +47,10 @@ int main(int argc, char* argv[]) switch (event.type) { case ENET_EVENT_TYPE_CONNECT: - printf ("A new client connected from %x:%u.\n", - event.peer -> address.host, + char clientname[1024]; + enet_address_get_host(&event.peer -> address,clientname, 1024); + printf ("A new client connected from %s:%u.\n", + clientname, event.peer -> address.port); /* Store any relevant client information here. */ event.peer -> data = "Client information";