<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
FONT-SIZE: 10pt;
FONT-FAMILY:Tahoma
}
</style>
</head>
<body class='hmmessage'>
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 <FONT color=#a31515>A new client connected from %x:%u.\n"</FONT><BR>
<BR>
But I do not receive the message....<BR>
<BR>
Similairly, when clients send data to the server, the case ENET_EVENT_TYPE_RECEIVE: is not activated.<BR>
<BR>
Do anyone know the reason based on the code below? Thanks a lot.<BR>
<BR>
<BR>
<BR>
Server code <BR>
<BR><FONT color=#0000ff size=2>
#include</FONT><FONT color=#000000 size=2> </FONT><FONT color=#a31515 size=2><stdio.h><BR></FONT><FONT color=#0000ff size=2>
#include</FONT><FONT color=#000000 size=2> </FONT><FONT color=#a31515 size=2><conio.h><BR></FONT><FONT color=#0000ff size=2>
#include</FONT><FONT color=#000000 size=2> </FONT><FONT color=#a31515 size=2>"enet/enet.h"<BR></FONT><FONT color=#0000ff size=2>
int</FONT><FONT color=#000000 size=2> main(</FONT><FONT color=#0000ff size=2>int</FONT><FONT color=#000000 size=2> argc, </FONT><FONT color=#0000ff size=2>char</FONT><FONT size=2><FONT color=#000000> ** argv)</FONT><BR>
{<BR>
</FONT><FONT color=#008000 size=2> //Initialize Enet<BR></FONT><FONT size=2>
</FONT><FONT color=#0000ff size=2> if</FONT><FONT size=2> (enet_initialize () != 0)<BR>
{<BR>
fprintf (stderr, </FONT><FONT color=#a31515 size=2>"An error occurred while initializing ENet.\n"</FONT><FONT size=2>);<BR>
</FONT><FONT color=#0000ff size=2> return</FONT><FONT size=2> EXIT_FAILURE;<BR>
}<BR>
<BR>
atexit (enet_deinitialize); </FONT><FONT color=#008000 size=2>//Deinitialize at exit <BR></FONT><FONT size=2>
</FONT><FONT color=#008000 size=2> //Creating an ENet server<BR></FONT><FONT size=2>
ENetAddress address;<BR>
ENetHost * server;<BR></FONT><FONT size=2>
address.host = ENET_HOST_ANY;<BR></FONT><FONT size=2>
address.port = 1234;<BR>
server = enet_host_create (&address </FONT><FONT color=#008000 size=2>/* the address to bind the server host to */</FONT><FONT size=2>, <BR>
32 </FONT><FONT color=#008000 size=2>/* allow up to 32 clients and/or outgoing connections */</FONT><FONT size=2>,<BR>
0 </FONT><FONT color=#008000 size=2>/* assume any amount of incoming bandwidth */</FONT><FONT size=2>,<BR>
0 </FONT><FONT color=#008000 size=2>/* assume any amount of outgoing bandwidth */</FONT><FONT size=2>);<BR>
</FONT><FONT color=#0000ff size=2> if</FONT><FONT size=2> (server == NULL)<BR>
{<BR>
fprintf(stderr, </FONT><FONT color=#a31515 size=2>"An error occurred while trying to create an ENet server host.\n"</FONT><FONT size=2>);<BR>
exit(EXIT_FAILURE);<BR>
}<BR>
</FONT><FONT color=#0000ff size=2> else<BR></FONT><FONT size=2>
fprintf (stderr, </FONT><FONT color=#a31515 size=2>"Server created\n"</FONT><FONT size=2>);<BR>
<BR>
<BR>
ENetEvent </FONT><FONT color=#0000ff size=2>event</FONT><FONT size=2>; <BR>
<BR>
</FONT><FONT color=#0000ff size=2> while</FONT><FONT size=2>(1)<BR>
{<BR>
</FONT><FONT color=#008000 size=2> /* Wait up to 1000 milliseconds for an event. */<BR></FONT><FONT size=2>
</FONT><FONT color=#0000ff size=2> if</FONT><FONT size=2>(enet_host_service (server, &</FONT><FONT color=#0000ff size=2>event</FONT><FONT size=2>, 1000) > 0)<BR>
{<BR>
</FONT><FONT color=#0000ff size=2> switch</FONT><FONT size=2> (</FONT><FONT color=#0000ff size=2>event</FONT><FONT size=2>.type)<BR>
{<BR>
</FONT><FONT color=#0000ff size=2> case</FONT><FONT size=2> ENET_EVENT_TYPE_CONNECT:<BR>
printf (</FONT><FONT color=#a31515 size=2>"A new client connected from %x:%u.\n"</FONT><FONT size=2>, <BR>
</FONT><FONT color=#0000ff size=2> event</FONT><FONT size=2>.peer -> address.host, <BR>
</FONT><FONT color=#0000ff size=2> event</FONT><FONT size=2>.peer -> address.port);<BR>
</FONT><FONT color=#008000 size=2> /* Store any relevant client information here. */<BR></FONT><FONT size=2>
</FONT><FONT color=#0000ff size=2> event</FONT><FONT size=2>.peer -> data = </FONT><FONT color=#a31515 size=2>"Client information"</FONT><FONT size=2>;<BR>
</FONT><FONT color=#0000ff size=2> break</FONT><FONT size=2>;</FONT><BR>
<FONT size=2> <BR>
</FONT><FONT color=#0000ff size=2> case</FONT><FONT size=2> ENET_EVENT_TYPE_RECEIVE:<BR>
printf (</FONT><FONT color=#a31515 size=2>"A packet of length %u containing %s was received from %s on channel %u.\n"</FONT><FONT size=2>,<BR>
</FONT><FONT color=#0000ff size=2> event</FONT><FONT size=2>.packet -> dataLength,<BR>
</FONT><FONT color=#0000ff size=2> event</FONT><FONT size=2>.packet -> data,<BR>
</FONT><FONT color=#0000ff size=2> event</FONT><FONT size=2>.peer -> data,<BR>
</FONT><FONT color=#0000ff size=2> event</FONT><FONT size=2>.channelID);<BR>
</FONT><FONT color=#008000 size=2></FONT> <BR>
<FONT color=#008000 size=2> /* Clean up the packet now that we're done using it. */<BR></FONT><FONT size=2>
enet_packet_destroy (</FONT><FONT color=#0000ff size=2>event</FONT><FONT size=2>.packet);<BR>
</FONT><FONT color=#0000ff size=2> break</FONT><FONT size=2>;<BR>
<BR>
</FONT><FONT color=#0000ff size=2></FONT> <BR>
<FONT color=#0000ff size=2> case</FONT><FONT size=2> ENET_EVENT_TYPE_DISCONNECT:<BR>
printf (</FONT><FONT color=#a31515 size=2>"%s disconected.\n"</FONT><FONT size=2>, </FONT><FONT color=#0000ff size=2>event</FONT><FONT size=2>.peer -> data);<BR>
</FONT><FONT color=#008000 size=2> /* Reset the peer's client information. */<BR></FONT><FONT size=2>
</FONT><FONT color=#0000ff size=2> event</FONT><FONT size=2>.peer -> data = NULL;<BR>
}<BR>
}<BR>
</FONT><FONT color=#0000ff size=2> else<BR></FONT><FONT size=2>
printf(</FONT><FONT color=#a31515 size=2>"No events received\n"</FONT><FONT size=2>);<BR>
}<BR></FONT><FONT size=2>
<BR>
</FONT><FONT color=#0000ff size=2> return</FONT><FONT size=2> 0;<BR>
}<BR>
<BR>
Client code <BR>
<BR><FONT color=#0000ff size=2>
#include</FONT><FONT color=#000000 size=2> </FONT><FONT color=#a31515 size=2><stdio.h><BR></FONT><FONT color=#0000ff size=2>
#include</FONT><FONT color=#000000 size=2> </FONT><FONT color=#a31515 size=2><conio.h><BR></FONT><FONT color=#0000ff size=2>
#include</FONT><FONT color=#000000 size=2> </FONT><FONT color=#a31515 size=2>"enet/enet.h"<BR></FONT><FONT color=#0000ff size=2>
int</FONT><FONT color=#000000 size=2> main(</FONT><FONT color=#0000ff size=2>void</FONT><FONT size=2><FONT color=#000000>)</FONT><BR>
{<BR>
</FONT><FONT color=#0000ff size=2> if</FONT><FONT size=2> (enet_initialize () != 0)<BR>
{<BR>
fprintf (stderr, </FONT><FONT color=#a31515 size=2>"An error occurred while initializing ENet.\n"</FONT><FONT size=2>);<BR>
</FONT><FONT color=#0000ff size=2> return</FONT><FONT size=2> EXIT_FAILURE;<BR>
}<BR>
atexit (enet_deinitialize); </FONT><FONT color=#008000 size=2>//Deinitialize at exit<BR></FONT><FONT size=2>
<BR>
ENetHost * client;<BR>
client = enet_host_create (NULL </FONT><FONT color=#008000 size=2>/* create a client host */</FONT><FONT size=2>,<BR>
1 </FONT><FONT color=#008000 size=2>/* only allow 1 outgoing connection */</FONT><FONT size=2>,<BR>
57600 / 8 </FONT><FONT color=#008000 size=2>/* 56K modem with 56 Kbps downstream bandwidth */</FONT><FONT size=2>,<BR>
14400 / 8 </FONT><FONT color=#008000 size=2>/* 56K modem with 14 Kbps upstream bandwidth */</FONT><FONT size=2>);<BR>
</FONT><FONT color=#0000ff size=2> if</FONT><FONT size=2> (client == NULL)<BR>
{<BR>
fprintf (stderr, </FONT><FONT color=#a31515 size=2>"An error occurred while trying to create an ENet client host.\n"</FONT><FONT size=2>);<BR>
exit (EXIT_FAILURE);<BR>
}<BR>
</FONT><FONT color=#0000ff size=2> else<BR></FONT><FONT size=2>
fprintf (stderr, </FONT><FONT color=#a31515 size=2>"Client created.\n"</FONT><FONT size=2>);<BR>
ENetAddress address;<BR>
ENetEvent </FONT><FONT color=#0000ff size=2>event</FONT><FONT size=2>;<BR>
ENetPeer *peer;<BR>
</FONT><FONT color=#008000 size=2> /* Connect to some.server.net:1234. */<BR></FONT><FONT size=2>
enet_address_set_host (&address, </FONT><FONT color=#a31515 size=2>"172.21.41.99"</FONT><FONT size=2>);<BR>
address.port = 1234;<BR>
</FONT><FONT color=#008000 size=2> /* Initiate the connection, allocating the two channels 0 and 1. */<BR></FONT><FONT size=2>
peer = enet_host_connect (client, &address, 2); <BR>
<BR>
</FONT><FONT color=#0000ff size=2> if</FONT><FONT size=2> (peer == NULL)<BR>
{<BR>
fprintf (stderr, </FONT><FONT color=#a31515 size=2>"No available peers for initiating an ENet connection.\n"</FONT><FONT size=2>);<BR>
exit (EXIT_FAILURE);<BR>
}<BR>
<BR>
</FONT><FONT color=#008000 size=2> </FONT><BR>
<FONT color=#008000 size=2> /* Wait up to 5 seconds for the connection attempt to succeed. */<BR></FONT><FONT size=2>
</FONT><FONT color=#0000ff size=2> if</FONT><FONT size=2> (enet_host_service (client, & </FONT><FONT color=#0000ff size=2>event</FONT><FONT size=2>, 5000) > 0 && </FONT><FONT color=#0000ff size=2>event</FONT><FONT size=2>.type == ENET_EVENT_TYPE_CONNECT)<BR>
{<BR>
puts (</FONT><FONT color=#a31515 size=2>"Connection to localhost:1234 succeeded."</FONT><FONT size=2>);<BR>
</FONT><FONT color=#008000 size=2> /* Create a reliable packet of size 7 containing "packet\0" */<BR></FONT><FONT size=2>
ENetPacket * packet = enet_packet_create (</FONT><FONT color=#a31515 size=2>"packet"</FONT><FONT size=2>, <BR>
strlen (</FONT><FONT color=#a31515 size=2>"packet"</FONT><FONT size=2>) + 1, <BR>
ENET_PACKET_FLAG_RELIABLE);<BR>
</FONT><FONT color=#008000 size=2> </FONT><BR>
<FONT color=#008000> </FONT><FONT size=2></FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> temp = enet_peer_send (peer, 0, packet);<BR>
</FONT><FONT color=#0000ff size=2> if</FONT><FONT size=2>(temp == 0)<BR>
printf(</FONT><FONT color=#a31515 size=2>"Send Successful\n"</FONT><FONT size=2>);<BR>
<BR>
<BR></FONT><FONT size=2>
}<BR>
</FONT><FONT color=#0000ff size=2> else<BR></FONT><FONT size=2>
{<BR></FONT><FONT size=2>
enet_peer_reset (peer);<BR>
puts (</FONT><FONT color=#a31515 size=2>"Connection to localhost:1234 failed."</FONT><FONT size=2>);<BR>
}<BR>
<BR>
</FONT><FONT size=2> <BR>
</FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2> 0;<BR>
<BR>
<BR>
}<BR></FONT></FONT><br /><hr />Share your beautiful moments with Photo Gallery. <a href='http://get.live.com/photogallery/overview' target='_new'>Windows Live Photo Gallery</a></body>
</html>