Enet Client Server Problem

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

Enet Client Server Problem

Post by ranar »

My Enet code dose not seem to be able to work correctly except on my pc. I am new to enet and i would prefer this over a high level networking lib. On my pc it works how it should but when i have the server running on lets say pc1 and a client connect to it from pc2 which is LAN it works. But when i run a client from pc1 with the server they both get disconnected saying null was disconnected.

I have not tried connecting outside of my home network though.

Here is my code i got of a site and added stuff and took out.
Server:

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;

}
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";
   getline(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);
                   printf ("why did this happen", event.peer -> data);


                   break;
               }
           }
           else if (serviceResult > 0)
           {
               puts ("Error with servicing the client");
               exit (EXIT_FAILURE);
           }

       }


       printf ("Sayy> ");
       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;

}

Btw this is my first time posting here so i dont know if this is the correct forum but i plan getting practice with this before i put it in ogre thanks. IF you have a client server program in enet that works i could use that and compare them to see how that works.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Enet Client Server Problem

Post by c6burns »

enet has a mailing list, you should try that for support
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

Re: Enet Client Server Problem

Post by ranar »

I have tried that and i dont know if im doing something wrong but it tells me i am not allowed to post.

Yup i just tried again and i got a email back that said i am not allowed to post in this mailing list please contact mailing list owner if you thing this is a error thing so i did. In the meantime anyone who uses enet could help with this? thanks.
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

Re: Enet Client Server Problem

Post by ranar »

Well i am not going to be able to post on that mailing list right now but have any of you used POCO? This looks like a good Network lib but i cant seem to compile it on windows for codeblocks and i dont really feel like using raknet or anything like that. Any one have history with POCO, enet, or SFML those are the 3 that i am interested in. Right now i was going to try and get SFML to work but i am having a problem with it and im to tired to try and fix it. It is a __gxx_personality_v0 error never heard of it but i found some stuff on how to fix it i will try tomorrow.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Enet Client Server Problem

Post by c6burns »

Why don't you feel like using raknet?
User avatar
areay
Bugbear
Posts: 819
Joined: Wed May 05, 2010 4:59 am
Location: Auckland, NZ
x 69

Re: Enet Client Server Problem

Post by areay »

I use Ogre3D and eNet, it works fine. I know many others have integrated it successfully too.

Looks like you've copied and pasted your code straight out of the eNet example so it should be working. When I first started with eNet I had no problem running one process as a server and another process as a client on the same PC, is that how you're doing it?

Question, why are you posting on the Ogre3D forums? Sounds like you're playing with SFML and eNet.
NotCamelCase
Greenskin
Posts: 140
Joined: Sun Feb 03, 2013 6:32 pm
x 8

Re: Enet Client Server Problem

Post by NotCamelCase »

ranar wrote:Btw this is my first time posting here so i dont know if this is the correct forum but i plan getting practice with this before i put it in ogre thanks. IF you have a client server program in enet that works i could use that and compare them to see how that works.
Stunt Rally which is based on OGRE uses Enet for networking, you can check out their sources.

https://code.google.com/p/vdrift-ogre/
https://github.com/stuntrally/stuntrally
Check out my projects: https://github.com/NotCamelCase
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

Re: Enet Client Server Problem

Post by ranar »

c6burns wrote:Why don't you feel like using raknet?
I guess i just wanted to have a low level network that i could go off and enet was perfect for that i thought. I also wanted something smaller that i could expand to fit what i needed.
areay wrote: Looks like you've copied and pasted your code straight out of the eNet example so it should be working. When I first started with eNet I had no problem running one process as a server and another process as a client on the same PC, is that how you're doing it?
Enet works great on my pc when i run the client and server but if i run the server on my pc and run the client on another that is when i have problems. I just got to test it outside of my network so i forwarded my router and had a friend connect to the server and it disconnected him the case switched to ENET_EVENT_TYPE_DISCONNECT im guessing since it posted on the cmd something disconnect which is in that case. This one that my friend and i were testing is a different client and server its smaller than the original one i posted but it is the same thing almost. Server:

Code: Select all

#include <iostream>
#include <enet/enet.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    ENetAddress address;
    ENetHost *server;
    ENetEvent event;
    int eventStatus;

    // a. Initialize enet
    if (enet_initialize() != 0) {
        fprintf(stderr, "An error occured while initializing ENet.\n");
        return EXIT_FAILURE;
    }

    atexit(enet_deinitialize);

    // b. Create a host using enet_host_create
    address.host = ENET_HOST_ANY;
    address.port = 1234;

    server = enet_host_create(&address, 32, 2, 0, 0);

    if (server == NULL) {
        fprintf(stderr, "An error occured while trying to create an ENet server host\n");
        exit(EXIT_FAILURE);
    }

    // c. Connect and user service
    eventStatus = 1;

    while (1) {
        eventStatus = enet_host_service(server, &event, 50000);

        // If we had some event that interested us
        if (eventStatus > 0) {
            switch(event.type) {
                case ENET_EVENT_TYPE_CONNECT:
                    printf("(Server) We got a new connection from %x\n",
                            event.peer->address.host);
                    break;

                case ENET_EVENT_TYPE_RECEIVE:
                    printf("(Server) Message from client : %s\n", event.packet->data);
                    // Lets broadcast this message to all
                    enet_host_broadcast(server, 0, event.packet);
                    break;

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

                    // Reset client's information
                    event.peer->data = NULL;
                    break;

            }
        }
    }

}
Client:

Code: Select all

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <enet/enet.h>

int main(int argc, char **argv)
{
    ENetAddress address;
    ENetHost *client;
    ENetPeer *peer;
    char message[1024];
    ENetEvent event;
    int eventStatus;

    // a. Initialize enet
    if (enet_initialize() != 0) {
        fprintf(stderr, "An error occured while initializing ENet.\n");
        return EXIT_FAILURE;
    }

    atexit(enet_deinitialize);

    // b. Create a host using enet_host_create
    client = enet_host_create(NULL, 1, 2, 57600/8, 14400/8);

    if (client == NULL) {
        fprintf(stderr, "An error occured while trying to create an ENet server host\n");
        exit(EXIT_FAILURE);
    }

    enet_address_set_host(&address, "192.168.1.145");
    address.port = 1234;

    // c. Connect and user service
    peer = enet_host_connect(client, &address, 2, 0);

    if (peer == NULL) {
        fprintf(stderr, "No available peers for initializing an ENet connection");
        exit(EXIT_FAILURE);
    }

    eventStatus = 1;

    while (1) {
        eventStatus = enet_host_service(client, &event, 0);

        // If we had some event that interested us
        if (eventStatus > 0) {
            switch(event.type) {
                case ENET_EVENT_TYPE_CONNECT:
                    printf("(Client) We got a new connection from %x\n", event.peer->address.host);
                    event.peer->data = (void*)"New User";
                    break;

                case ENET_EVENT_TYPE_RECEIVE:
                    printf("(Client) Message from server : %s\n", event.packet->data);
                    // Lets broadcast this message to all
                    // enet_host_broadcast(client, 0, event.packet);
                    enet_packet_destroy(event.packet);
                    break;

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

                    // Reset client's information
                    event.peer->data = NULL;
                    break;
            }
        }

        printf("Say > ");
        gets(message);

        if (strlen(message) > 0) {
            ENetPacket *packet = enet_packet_create(message, strlen(message) + 1, ENET_PACKET_FLAG_RELIABLE);
            enet_peer_send(peer, 0, packet);
        }
    }
}
areay wrote:Question, why are you posting on the Ogre3D forums? Sounds like you're playing with SFML and eNet.
Nope i was trying the network part of SFML still using OGRE :)
NotCamelCase wrote: Stunt Rally which is based on OGRE uses Enet for networking, you can check out their sources.

https://code.google.com/p/vdrift-ogre/
https://github.com/stuntrally/stuntrally
I will take a look at that i was looking at the cube project which i hear is why enet was made but there network code follows mine i think from what i saw. im stumped on why people cant stay connected to the server.

BTW if anyone is interested themselves on how they do that here you go. http://cubeengine.com/
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

Re: Enet Client Server Problem

Post by ranar »

Okay so i have been told that i should try to create a thread for servicing and another for sending packets as you can read here if you wish on the enet mailing list(found out that aol email wont let you post on the mailing list). I have never used threading before and i dont know where to begin can any of you lead me in the right direction?
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Enet Client Server Problem

Post by c6burns »

Begin with google like the dude on the mailing list suggested? Try boost::thread, or you could try std::thread if your compiler has c++11 support

It's all well and good to want a low level networking library for whatever reason, like if you just want to learn more about networking, or feel that you absolutely need it for performance, or your friends think you are cool for implementing it, or your favorite game implemented it. But have you looked seriously at RakNet, because it handles threading for you. It also handles object replication and other issues you will end up facing depending on the needs of your implementation. Just making a suggestion and not trying to be a douche, so please don't take this the wrong way :)
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

Re: Enet Client Server Problem

Post by ranar »

c6burns wrote:But have you looked seriously at RakNet, because it handles threading for you. It also handles object replication and other issues you will end up facing depending on the needs of your implementation. Just making a suggestion and not trying to be a douche, so please don't take this the wrong way :)
your not being a douche, although I can not say i have looked at RakNet Seriously because i cant get it to work with code blocks. Is the file only so big because it has a lot of examples? Because for me when just testing a chat and server with a friend i should not need to have my friend download a file that is bigger than 20mb which is not big but i don't want him thinking I'm filling his PC up with junk. That is another reason why i like enet its small but yeah you are right it is going to be more time consuming to implement features, I'm up for the task.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Enet Client Server Problem

Post by Kojack »

ranar wrote: Is the file only so big because it has a lot of examples? Because for me when just testing a chat and server with a friend i should not need to have my friend download a file that is bigger than 20mb which is not big but i don't want him thinking I'm filling his PC up with junk.
I haven't looked at the latest version, but 2 years ago my students made a game with raknet and it only required a 1MB dll.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Enet Client Server Problem

Post by c6burns »

The static lib is around 20 megs, but unless you are somehow using an Atari XL complete with 300 baud modem (in which case I will duel you at Dr J vs Larry Bird), I don't think it's an unreasonable size. Also, your executable is only going to link in what you use of the library if you go the static route, which I imagine would be nowhere close to 20 megs.
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

Re: Enet Client Server Problem

Post by ranar »

Well i will give it a try but i will comeback to enet maybe raknet can teach me a few things and help me with enet? Now i have to find a tutorial on raknet and codeblocks
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 114

Re: Enet Client Server Problem

Post by mkultra333 »

I looked at Raknet years ago and didn't like the license. It basically said "You can use this for free until we say otherwise, and then it'll cost you," and when I emailed for better info on just when it would or wouldn't cost me, and how much, they refused to say.

I just had a look again now, and according to http://www.jenkinssoftware.com/pricing.html it's free until you hit $100,000 revenue... and then it's $8,000. Jeez, that's 8%, pretty steep! ($16,000 if you sell the game for $30 or more.) And I'm assuming that's before the publishers and anyone else have taken their cut too. So it's fine if you game is a failure, or a mega hit. But if you have a modestly successful game, it'll cost you big. Unless you have a big budget, I think you're better off learning to use something like enet, at least it won't come back to bite you if you make $100K.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Enet Client Server Problem

Post by c6burns »

I don't see 8k once you've made 100k in gross revenue as a problem, but I suppose that's up to the implementer to decide. If your time is worth money, then the software is worth the license fee. My suggestion was coming from a "you've never used threading so here's a really good library to solve your problems" space.

I can't speak to the fact they never answered your email, that's kinda creepy and I hate when companies do that, but the license agreement is available to read in full and isn't that hard to parse through the legalese. It boils down to "buy a commercial license when your gross revenue exceeds $100k" and "allow us to examine your accounting if we request". Anyway, it's not like I'm getting a commission if you use it ... it's just a really good library.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 114

Re: Enet Client Server Problem

Post by mkultra333 »

I don't see 8k once you've made 100k in gross revenue as a problem, but I suppose that's up to the implementer to decide.
Well, let's say you work alone (no need to pay artist or other programmers) and just manage to make 100k. Steam or whoever is going to want at least a third of that, so you're down to 70K. And of course you'll have to pay tax, that'll vary so let's say 20%. Now you're down to 56K. Now take out the 8k and you're on just 48K.

8K out of 56K is 14%. That is a pretty big chunk for just one library. Although I suppose getting exactly 100k is the worst case scenario, if you get significantly more it won't be so bad, and a little less and you won't have to pay anything. I guess I'd like their license better if it didn't hit all at once when you hit a magic number. If it started to fade in from some earlier value and then peaked at some point. 8k at the 100k mark just seems too much to me, pretty hard for a barely successful indie to afford.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

Re: Enet Client Server Problem

Post by ranar »

Okay maybe sometime this week i can post code of me attempting threading with the SFML Threading library. I havent tried it yet but will soon hopefully since i cant get raknet to work which is fine by me right now.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Enet Client Server Problem

Post by c6burns »

mkultra333 wrote:Although I suppose getting exactly 100k is the worst case scenario, if you get significantly more it won't be so bad, and a little less and you won't have to pay anything. I guess I'd like their license better if it didn't hit all at once when you hit a magic number. If it started to fade in from some earlier value and then peaked at some point. 8k at the 100k mark just seems too much to me, pretty hard for a barely successful indie to afford.
I see your point there about worst case scenario. Maybe it's just that I've slaved on binary network protos in the past and have no desire to ever do it again, so the library seems more valuable to me than to someone bright eyed and bushy tailed for that work :lol: I agree they could go even further to consider indies in their pricing, but compared to many middleware packages they've done a lot. I can't even name names because I can't remember which ones made me sign NDAs so that I couldn't talk about how poorly structured their licensing fees are :?
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

Re: Enet Client Server Problem

Post by ranar »

I have used SFML for threading and i have got the code to work over lan at least. There were no disconnections and before when i said i would post the code, well it is quite messy right now and i will post it when i get a chance.
Post Reply