Here's the c/s model example to use enet library:<br><br>Server:<br><br>#include <winsock2.h><br>#include "stdafx.h"<br>#include "../enet/enet.h"<br>#include <iostream><br>using namespace std;<br>
<br><br>#pragma comment(lib, "ws2_32.lib")<br>#pragma comment(lib, "enet.lib")<br>#pragma comment(lib, "winmm" )<br><br><br>int main(int argc, _TCHAR* argv[])<br>{<br> enet_initialize (); <br>
ENetAddress address;<br> ENetHost * server;<br><br> address.host = ENET_HOST_ANY; <br> address.port = 5555;<br><br> server = enet_host_create (& address, 32, 0, 0);<br><br> if(server == NULL)<br> {<br>
cout<<"create failed"<<endl;<br> exit (EXIT_FAILURE);<br> }<br><br> ENetEvent event;<br> int ret = 0;<br><br> while (1)<br> {<br> ret = enet_host_service (server, & event, 1000);<br>
while ( ret > 0)<br> {<br> switch (event.type)<br> {<br> case ENET_EVENT_TYPE_CONNECT:<br> printf ("A new client connected from %x:%u.\n", <br> event.peer -> address.host,<br>
event.peer -> address.port);<br><br> event.peer -> data = "Client information";<br><br> break;<br><br> case ENET_EVENT_TYPE_RECEIVE:<br> printf ("A packet of length %u containing %s was received from %s on channel %u.\n",<br>
event.packet -> dataLength,<br> event.packet -> data,<br> event.peer -> data,<br> event.channelID);<br><br> enet_packet_destroy (event.packet);<br>
<br> break;<br><br> case ENET_EVENT_TYPE_DISCONNECT:<br> printf ("%s disconected.\n", event.peer -> data);<br><br> event.peer -> data = NULL;<br> }<br> } <br>
}<br><br> enet_host_destroy(server);<br> <br> enet_deinitialize();<br><br> return 0;<br>}<br><br><br>/**************************************************************************************/<br>Client:<br><br>#include <winsock2.h><br>
#include "stdafx.h"<br>#include "enet/enet.h"<br>#include <iostream><br>using namespace std;<br><br><br>#pragma comment(lib, "ws2_32.lib")<br>#pragma comment(lib, "enet.lib")<br>
#pragma comment(lib, "winmm" )<br><br><br>int main(int argc, _TCHAR* argv[])<br>{<br> enet_initialize (); //initialize the library<br><br> ENetHost * client;<br><br> client = enet_host_create (NULL, 10, 57600, 14400);<br>
<br> if(client == NULL)<br> {<br> cout<<"Client create failed"<<endl;<br> exit (EXIT_FAILURE);<br> }<br><br> ENetAddress address;<br> ENetPeer *peer;<br><br> enet_address_set_host (& address, "<a href="http://127.0.0.1">127.0.0.1</a>") ; <br>
address.port = 5555;<br><br> while (peer = enet_host_connect (client, & address, 2), peer == NULL)<br> {<br> Sleep(100);<br> }<br> <br><br> ENetPacket * packet = enet_packet_create ("packet", <br>
strlen ("packet") + 1, <br> ENET_PACKET_FLAG_RELIABLE);<br><br> for (int i = 0; i < 100; i++)<br> {<br> cout<<enet_peer_send (peer, 0, packet)<<endl;<br> } <br><br> enet_peer_disconnect (peer, 0);<br>
<br> enet_host_destroy(client);<br><br> enet_deinitialize(); //clean up the librar<br><br> return 0;<br>}<br><br><br><br>I run the Server first,then run the client;<br>Problem1:The client showed that enet_host_connect() return a nonull peer, but the server had no action;<br>
Problem2: The client' enet_peer_send() always returned -1;<br> i don't know why ? wait for help <br><br>thansks <br>