[ENet-discuss] help needed for a client/server example
Schwarzbauer Martin - S0810567019
S0810567019 at students.fh-hagenberg.at
Thu Jan 14 04:58:45 PST 2010
Hi !
i'm experimenting with the Enet-Library ... so i tried to write a simple client - server test application.
I also studied the Tutorial page but i don't know how to send/receive data. When the server-app is running, the client can "connect" to the server ("Connection to server succeeded." is displayed).
Now, how can i send data between server and client, after the client is connected or how does the server know that a client connected?
Server:
if (enet_initialize () != 0)
{
fprintf (stderr, "An error occurred while initializing ENet.\n");
return EXIT_FAILURE;
}
atexit (enet_deinitialize);
ENetAddress address;
ENetHost * server;
ENetEvent event;
/* Bind the server to the default localhost. */
/* A specific host address can be specified by */
// enet_address_set_host (& address, "localhost");
address.host = ENET_HOST_ANY;
/* Bind the server to port 1234. */
address.port = 1234;
server = enet_host_create (&address /* the address to bind the server host to */,
1 /* 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);
}
enet_host_destroy(server);
Client:
if (enet_initialize () != 0)
{
fprintf (stderr, "An error occurred while initializing ENet.\n");
return EXIT_FAILURE;
}
atexit (enet_deinitialize);
ENetAddress address;
ENetHost * client;
ENetPeer *peer;
ENetEvent event;
/* Connect to localhost:1234. */
enet_address_set_host (& address, "localhost");
address.port = 1234;
client = enet_host_create (NULL /* create a client host */,
1 /* allow only 1 outgoing connection */,
0 /* assume any amount of incoming bandwidth */,
0 /* assume any amount of outgoing bandwidth */);
if (client == NULL)
{
fprintf (stderr,
"An error occurred while trying to create an ENet client host.\n");
exit (EXIT_FAILURE);
}
/* Initiate the connection, allocating one channel */
peer = enet_host_connect (client, &address, 1);
if (peer == NULL) {
fprintf (stderr,
"No available peers for initiating an ENet connection.\n");
enet_host_destroy(client);
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 server succeeded.");
}
else
{
/* Either the 5 seconds are up or a disconnect event was */
/* received. Reset the peer in the event the 5 seconds */
/* had run out without any significant event. */
enet_peer_reset (peer);
puts ("Connection to server failed.");
}
enet_host_destroy(client);
thx in advance,
Martin
PS: maybe it would be a idea to insert a full server - client - example onto the homepage.
More information about the ENet-discuss
mailing list