Need help with my Enet code

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Oster200
Posts: 60
Joined: Sun May 06, 2012 6:13 pm

Need help with my Enet code

Post by Oster200 »

Hello everyone i have a couple questions for the people who have used the Enet networking lib, i have found this on the web to and changed a few things.
Before i get started here is my server code:

Code: Select all

/* server.cpp */
#include <stdio.h>
#include <enet/enet.h>
 
int main (int argc, char *argv[])
{
   ENetAddress address;
   ENetHost *server;
   ENetEvent event;
   int serviceResult;
 
   puts ("Starting server");
 
   if (enet_initialize () != 0)
   {
       puts ("Error initialising enet");
       exit (EXIT_FAILURE);
   }
   else puts("Initialised enet");
 
 
   /* Bind the server to the default localhost.     */
   /* A specific host address can be specified by   */
   /* enet_address_set_host (& address, "x.x.x.x"); */
   address.host = ENET_HOST_ANY;
   puts("Bound Server to localhost");
   /* Bind the server to port 1234. */
   address.port = 1234;
   puts("Bound Server to Port 1234");
 
   server = enet_host_create (&address,
                             32,   /* number of clients */
                             2,    /* number of channels */
                             0,    /* Any incoming bandwith */
                             0);   /* Any outgoing bandwith */
 
   if (server == NULL)
   {
       puts ("Could not create server host");
       exit (EXIT_FAILURE);
   }
   else puts("\nCreated Server and Ready for Clients");
 
 
   while (true)
   {
       serviceResult = 1;
 
       /* Keep doing host_service until no events are left */
       while (serviceResult > 0)
       {
           /* Wait up to 1000 milliseconds for an event. */
           serviceResult = enet_host_service (server, &event, 1000);
 
           if (serviceResult > 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 = (void*)"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);
 
                   /* Tell all clients about this message */
                   enet_host_broadcast (server, 0, 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;
 
                   break;
               }
           }
           else if (serviceResult > 0)
           {
               puts ("Error with servicing the server");
               exit (EXIT_FAILURE);
           }
       }
 
   }
 
   enet_host_destroy (server);
   enet_deinitialize ();
 
   return 0;
 
}
 
And here is the Client:

Code: Select all

/* client.cpp */
#include <stdio.h>
#include <string.h>
#include <enet/enet.h>
#include <iostream>
 
using namespace std;
 
 
int main (int argc, char* argv[]) {
   ENetHost *client;
   ENetAddress address;
   ENetPeer *peer;
   ENetEvent event;
   char message[1024];
   int serviceResult;
   string connectip;
 
   puts ("Starting client");
 
   if (enet_initialize () != 0) {
       fprintf (stderr, "Error initialising enet");
       exit (EXIT_FAILURE);
   }
 
   client = enet_host_create (NULL, /* create a client host */
                              1,    /* number of clients */
                              2,    /* number of channels */
                              57600 / 8,    /* incoming bandwith */
                              14400 / 8);   /* outgoing bandwith */
 
   if (client == NULL) {
       fprintf (stderr, "Could not create client host");
       exit (EXIT_FAILURE);
   }
 
 
   //cout << "Please enter the IP address you want to connect to.\n";
   //cin >> connectip;
 
   //const char * c = connectip.c_str();
   //enet_address_set_host (&address, c);
 
   enet_address_set_host (&address, "localhost");
   address.port = 1234;
 
   peer = enet_host_connect (client,
                             &address,    /* address to connect to */
                             2,           /* number of channels */
                             0);          /* user data supplied to the receiving host */
 
   if (peer == NULL) {
       fprintf (stderr, "No available peers for initiating an ENet "
                "connection.\n");
       exit (EXIT_FAILURE);
   }
 
 
   /* Try to connect to server within 5 seconds */
   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);
 
       fprintf (stderr, "Connection to server failed.");
       exit (EXIT_FAILURE);
   }
 
   while (true)
   {
       serviceResult = 1;
 
       /* Keep doing host_service until no events are left */
       while (serviceResult > 0)
       {
           serviceResult = enet_host_service (client, &event, 0);
 
           if (serviceResult > 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 = (void*)"New User";
                   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);
 
                   break;
               }
           }
           else if (serviceResult > 0)
           {
               puts ("Error with servicing the client");
               exit (EXIT_FAILURE);
           }
 
       }
 
 
       printf ("Say> ");
       gets (message);
 
       if (strcmp (message, "exit") == 0 ||
           strcmp (message, "quit") == 0) {
           break;
       }
 
       if(strlen(message) > 0) {
           ENetPacket *packet = enet_packet_create (message, strlen
(message) + 1, ENET_PACKET_FLAG_RELIABLE);
           enet_peer_send (peer, 0, packet);
       }
 
   }
 
   enet_peer_disconnect (peer, 0);
 
   /* Allow up to 3 seconds for the disconnect to succeed */
   /* and drop any packets received packets */
   while (enet_host_service (client, & event, 3000) > 0)
   {
 
       switch (event.type)
       {
       case ENET_EVENT_TYPE_RECEIVE:
           enet_packet_destroy (event.packet);
           break;
 
       case ENET_EVENT_TYPE_DISCONNECT:
           puts ("Disconnection succeeded.");
           break;
       }
   }
 
 
   enet_host_destroy (client);
   enet_deinitialize ();
 
   return 0;
 
}
 
Now to get to the questions.
Right now i am trying to get my client to connect with a different pc on lan that is running the server. In doing so i needed to have the client ask what ip address it should connect to so i added a string that would convert to a const char and use that as the ip address as shown here:

Code: Select all

//cout << "Please enter the IP address you want to connect to.\n";
   //cin >> connectip;
 
   //const char * c = connectip.c_str();
   //enet_address_set_host (&address, c);
 
   enet_address_set_host (&address, "localhost");
   address.port = 1234;
 
   peer = enet_host_connect (client,
                             &address,    /* address to connect to */
                             2,           /* number of channels */
                             0);          /* user data supplied to the receiving host */
As you can see i have it commented out because when i use it and replace enet_address_set_host with the one that is commented out it comes up with Say> twice which is a answer that can be answered later. My main problem is that it connects i think but it does not broadcast the message like what is codded in the server here:

Code: Select all

 /* Tell all clients about this message */
                   enet_host_broadcast (server, 0, event.packet);
I have tried to give you as much information as i could and as detailed as i could, If you have a chat example that works and you dont care sharing i can go of that. Also this is my first time working with enet.

Thanks
Oster200
Posts: 60
Joined: Sun May 06, 2012 6:13 pm

Re: Need help with my Enet code

Post by Oster200 »

Well i found out why my program outputs say> twice i just dont know how to fix it. It has to deal with cin >> i dont know why but when i dont have cin it only says it once it is like it is going through the loop twice.

Edit i changed cin to getline(cin,connectip);

Here is some more info on the main problem which is not being able to connect and talk over lan.

I run the server on this pc, and a client lets say client A. i run another client on a different pc lets say client B.

both the clients connect to the server but when i type on the client B the message gets sent to the server but dosent broadcast it. i than type somehting on client A but get a message that says (NULL) Disconnected. Than the other client has the same problem. This is my first time using Enet so things may be poorly done. thanks
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Need help with my Enet code

Post by mongoose7 »

You've got printf's in the code. Why don't you use their output to solve your problem?
Oster200
Posts: 60
Joined: Sun May 06, 2012 6:13 pm

Re: Need help with my Enet code

Post by Oster200 »

Okay well i have done just that and i have located the main problem but since i am new to enet i dont quite now how to fix it here is the update, i found that somehow the client gets disconnected. The case switches to ENET_EVENT_TYPE_DISCONNECT i think and disconnects it but i dont know how to stop it from doing that.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Need help with my Enet code

Post by mongoose7 »

Then debug the server. Etc.
Oster200
Posts: 60
Joined: Sun May 06, 2012 6:13 pm

Re: Need help with my Enet code

Post by Oster200 »

I dont know what to do i cant seem to get this to work and i cant seem to get a different networking lib to work like POCO on codeblocks.
Post Reply