[ENet-discuss] Anybody can give me a example code of enet?

xuxingjian xuxingjian at jyd.com.cn
Wed Jun 14 03:28:39 PDT 2006


I write a data send and receive demo.but I foud server can recv a few packet and hang up.

client  main code:

packet = enet_packet_create ("packet", 
                                              strlen ("packet") + 1, 
                                              ENET_PACKET_FLAG_RELIABLE);
while(1)
{
enet_peer_send (peer, 0, packet);
enet_host_flush( client);
}


server main code:

/* Wait up to 1000 milliseconds for an event. */
 while (enet_host_service (server, &event, 60) >= 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);

   /* Store any relevant client information here. */
   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);

   /* Clean up the packet now that we're done using it. */
   enet_packet_destroy (event.packet);

   break;

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

   /* Reset the peer's client information. */

   event.peer -> data = NULL;
  }
 }

output:
A new client connected from cc00a8c0:1062.
A packet of length 11 containing testpacket was received from Client informatio
A packet of length 11 containing testpacket was received from Client informatio
A packet of length 11 containing testpacket was received from Client informatio
A packet of length 11 containing testpacket was received from Client informatio
A packet of length 11 containing testpacket was received from Client informatio
A packet of length 11 containing testpacket was received from Client informatio
A packet of length 11 containing testpacket was received from Client informatio
A packet of length 11 containing testpacket was received from Client informatio

hang up.....


Any help is welcome!



More information about the ENet-discuss mailing list