[ENet-discuss] Cannot receive connection packet from client to server
Ng Yao Kheng
ngyaokheng at hotmail.com
Thu May 8 19:57:14 PDT 2008
I am following the tutorial to create a client and server program. When Client connection to the server, the server should go to case ENET_EVENT_TYPE_CONNECT: and print A new client connected from %x:%u.\n"
But I do not receive the message....
Similairly, when clients send data to the server, the case ENET_EVENT_TYPE_RECEIVE: is not activated.
Do anyone know the reason based on the code below? Thanks a lot.
Server code
#include <stdio.h>
#include <conio.h>
#include "enet/enet.h"
int main(int argc, char ** argv)
{
//Initialize Enet
if (enet_initialize () != 0)
{
fprintf (stderr, "An error occurred while initializing ENet.\n");
return EXIT_FAILURE;
}
atexit (enet_deinitialize); //Deinitialize at exit
//Creating an ENet server
ENetAddress address;
ENetHost * server;
address.host = ENET_HOST_ANY;
address.port = 1234;
server = enet_host_create (&address /* the address to bind the server host to */,
32 /* allow up to 32 clients and/or outgoing connections */,
0 /* assume any amount of incoming bandwidth */,
0 /* assume any amount of outgoing bandwidth */);
if (server == NULL)
{
fprintf(stderr, "An error occurred while trying to create an ENet server host.\n");
exit(EXIT_FAILURE);
}
else
fprintf (stderr, "Server created\n");
ENetEvent event;
while(1)
{
/* Wait up to 1000 milliseconds for an event. */
if(enet_host_service (server, &event, 1000) > 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;
}
}
else
printf("No events received\n");
}
return 0;
}
Client code
#include <stdio.h>
#include <conio.h>
#include "enet/enet.h"
int main(void)
{
if (enet_initialize () != 0)
{
fprintf (stderr, "An error occurred while initializing ENet.\n");
return EXIT_FAILURE;
}
atexit (enet_deinitialize); //Deinitialize at exit
ENetHost * client;
client = enet_host_create (NULL /* create a client host */,
1 /* only allow 1 outgoing connection */,
57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
if (client == NULL)
{
fprintf (stderr, "An error occurred while trying to create an ENet client host.\n");
exit (EXIT_FAILURE);
}
else
fprintf (stderr, "Client created.\n");
ENetAddress address;
ENetEvent event;
ENetPeer *peer;
/* Connect to some.server.net:1234. */
enet_address_set_host (&address, "172.21.41.99");
address.port = 1234;
/* Initiate the connection, allocating the two channels 0 and 1. */
peer = enet_host_connect (client, &address, 2);
if (peer == NULL)
{
fprintf (stderr, "No available peers for initiating an ENet connection.\n");
exit (EXIT_FAILURE);
}
/* Wait up to 5 seconds for the connection attempt to succeed. */
if (enet_host_service (client, & event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT)
{
puts ("Connection to localhost:1234 succeeded.");
/* Create a reliable packet of size 7 containing "packet\0" */
ENetPacket * packet = enet_packet_create ("packet",
strlen ("packet") + 1,
ENET_PACKET_FLAG_RELIABLE);
int temp = enet_peer_send (peer, 0, packet);
if(temp == 0)
printf("Send Successful\n");
}
else
{
enet_peer_reset (peer);
puts ("Connection to localhost:1234 failed.");
}
return 0;
}
_________________________________________________________________
Manage multiple email accounts with Windows Live Mail effortlessly.
http://www.get.live.com/wl/all
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cubik.org/pipermail/enet-discuss/attachments/20080509/21908fe8/attachment.htm
More information about the ENet-discuss
mailing list