[ENet-discuss] help

peng zou samneg at gmail.com
Thu Apr 24 23:41:54 PDT 2008


Here's the c/s model  example to use enet library:

Server:

#include <winsock2.h>
#include "stdafx.h"
#include "../enet/enet.h"
#include <iostream>
using namespace std;


#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "enet.lib")
#pragma comment(lib, "winmm" )


int main(int argc, _TCHAR* argv[])
{
   enet_initialize ();
   ENetAddress address;
   ENetHost * server;

   address.host = ENET_HOST_ANY;
   address.port = 5555;

   server = enet_host_create (& address, 32, 0, 0);

   if(server == NULL)
   {
      cout<<"create failed"<<endl;
      exit (EXIT_FAILURE);
   }

   ENetEvent event;
   int ret = 0;

   while (1)
   {
      ret = enet_host_service (server, & event, 1000);
      while ( ret > 0)
      {
         switch (event.type)
         {
         case ENET_EVENT_TYPE_CONNECT:
            printf ("A new client connected from %x:%u.\n",
               event.peer -> address.host,
               event.peer -> address.port);

            event.peer -> data = "Client information";

            break;

         case ENET_EVENT_TYPE_RECEIVE:
            printf ("A packet of length %u containing %s was received from
%s on channel %u.\n",
               event.packet -> dataLength,
               event.packet -> data,
               event.peer -> data,
               event.channelID);

            enet_packet_destroy (event.packet);

            break;

         case ENET_EVENT_TYPE_DISCONNECT:
            printf ("%s disconected.\n", event.peer -> data);

            event.peer -> data = NULL;
         }
      }
   }

   enet_host_destroy(server);

   enet_deinitialize();

   return 0;
}


/**************************************************************************************/
Client:

#include <winsock2.h>
#include "stdafx.h"
#include "enet/enet.h"
#include <iostream>
using namespace std;


#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "enet.lib")
#pragma comment(lib, "winmm" )


int main(int argc, _TCHAR* argv[])
{
   enet_initialize ();  //initialize the library

   ENetHost * client;

   client = enet_host_create (NULL, 10, 57600, 14400);

   if(client == NULL)
   {
      cout<<"Client create failed"<<endl;
      exit (EXIT_FAILURE);
   }

   ENetAddress address;
   ENetPeer *peer;

   enet_address_set_host (& address, "127.0.0.1") ;
   address.port = 5555;

   while (peer = enet_host_connect (client, & address, 2), peer == NULL)
   {
      Sleep(100);
   }


   ENetPacket * packet = enet_packet_create ("packet",
      strlen ("packet") + 1,
      ENET_PACKET_FLAG_RELIABLE);

   for (int i = 0; i < 100; i++)
   {
      cout<<enet_peer_send (peer, 0, packet)<<endl;
   }

   enet_peer_disconnect (peer, 0);

   enet_host_destroy(client);

   enet_deinitialize();  //clean up the librar

   return 0;
}



I run the Server first,then run the client;
Problem1:The client  showed that enet_host_connect() return a nonull peer,
but the server had no action;
Problem2: The client' enet_peer_send() always returned -1;
 i don't know why ?  wait for help

thansks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cubik.org/pipermail/enet-discuss/attachments/20080425/53303117/attachment.htm 


More information about the ENet-discuss mailing list