Jump to content
  • Advertisement
Sign in to follow this  
Josh Klint

[Resolved] Simple enet program fails

This topic is 2779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a Lua debugger using enet to communicate between processes. The debugger program is written in another language (but uses enet!), and works with a test executable also written in that language.

Now I am trying to add code to my C++ program to make it communicate with the debugger program. I have a simple program like this:
if (enet_initialize()!=0)
{
Print("Failed to initialize enet.");
}
else
{
ENetAddress address;
ENetEvent enetevent;
address.host = ENET_HOST_ANY;
address.port = 7776;
InterpreterDebugHost = enet_host_create(&address,64,0,0);
if (InterpreterDebugHost==NULL)
{
Print("Failed to create host on port "+String(address.port));
}
else
{
Print("Checking for events loop...");
bool exitloop = false;
int time = Millisecs();
while (true)
{
int result = enet_host_service(InterpreterDebugHost,&enetevent,0);
if (result>0)
{
switch (enetevent.type)
{
case ENET_EVENT_TYPE_CONNECT:
Print("Connect");
exitloop=true;
break;
case ENET_EVENT_TYPE_DISCONNECT:
Print("Disconnect");
break;
case ENET_EVENT_TYPE_RECEIVE:
Print("Receive");
break;
case ENET_EVENT_TYPE_NONE:
Print("No event");
break;
}
}
else
{
if (result<0) Print("enet_host_service failed.");
}
if (Millisecs()-time>10000) exitloop = true;
if (exitloop) break;
}
Print("Done with events loop");
}
}
}

For some reason, the C++ program never detects any events, even though it is pretty much identical to my other test program not written in C++.

Share this post


Link to post
Share on other sites
Advertisement
The code looks fine, you should try to verify that the problem is not on the other side, i.e. the client is not even attempting to connect. Here is my main loop just for your own sanity, which is basically a copy and paste from the enet web site. The below code works, so it has to be something else

ENetHost* Host=0;
void RunNetwork(uint32_t waittimeinms){
ENetEvent event;
/* Wait up to waittimeinms milliseconds for an event. */
while (enet_host_service (Host, &event, waittimeinms) > 0) {// this will send and receive data
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE:

Receive(event);
enet_packet_destroy (event.packet); //Clean up the packet now that we're done using it.
break;
case ENET_EVENT_TYPE_CONNECT:

Connect(event);
break;
case ENET_EVENT_TYPE_DISCONNECT:// this can be a timeout, or a regular disconnect
Disconnect(event);
break;
}
}
}
// and my server initi code
void InitServer(){
if(Host) enet_host_destroy(Host); // clean up the exisitng host and continue
ENetAddress address;
address.host = ENET_HOST_ANY;
address.port =port;
Host = enet_host_create (&address, /* create a client host */
20, /* numer of connections */
2, /* number of channels: channel 0 is for normal data, and channel 1 is for encrpyted data */
0, //incomingbandwith
0);//outgoingbandwith
if (Host == NULL){
OUTPUT_DEBUG_MSG("An error occurred while trying to create an ENet Server host.");
return false;
}
}

Share this post


Link to post
Share on other sites
Why does your enet_host_create() command take five arguments, and mine only takes four? I downloaded enet 1.2.3.

I have verified the other program works. I have another test program not written in C++ that responds when my debugger program attempts to connect.

I also tried doing it the other way, having the test program connect to the debugger. Same results.

My firewall does say the C++ program is trying to connect (and I let it, of course). Could it be a problem with me including a wrong library or something like that? I can't see any reason the code itself would have a problem.

I added these additional libraries in the project settings:
ws2_32.lib
winmm.lib

Share this post


Link to post
Share on other sites
I was using two different incompatible versions of enet, but after fixing that problem, behavior is unchanged...

Share this post


Link to post
Share on other sites
Test to see if any data is being read in to enet by going to the function enet_protocol_receive_incoming_commands in the protocol.c file and change the function to add some output if any data is read in. This way you can know if any data is even being read.

The latest version of enet is 1.31 so make sure you and the client are using the same versions. Also, if you are building dlls, or lib files and linking them make sure you rebuild them for the appropriate version. I just compile the source in my project, I hate dlls and libs.

Share this post


Link to post
Share on other sites
Below are two simple C++ programs based on the tutorial here:
http://enet.bespin.org/Tutorial.html

They never connect! This isn't right.

I also notice if I unplug my network cable, my C++ client program gives this error:
Error sending outgoing packets: No error[/quote]

My BlitzMax programs, that are importing the exact same .c files (and compiling them with GCC) doesn't have any problem connecting when the network cable is plugged in or unplugged.

Here is the source for my C++ server and client programs:

Server.cpp:
int main()
{
ENetEvent event;
ENetAddress address;
ENetHost* server;

enet_initialize();

address.host = ENET_HOST_ANY;
address.port = 1234;

server = enet_host_create (&address,32,2,0,0);
if (server == NULL)
{
Print("An error occurred while trying to create an ENet server host.");
return 1;
}

int time = Millisecs();
while(true)
{
while (enet_host_service (server, &event,0) > 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 = "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);
enet_packet_destroy (event.packet);
break;

case ENET_EVENT_TYPE_DISCONNECT:
printf ("%s disconected.\n", event.peer -> data);
event.peer -> data = NULL;
}
}
if (Millisecs()-time>10000) break;
}
Print("Nothing happened, I give up.");
Delay(3000);
return 0;
}


Client.cpp:
int main()
{
ENetEvent event;
ENetHost * client;
ENetAddress address;

enet_initialize();

client = enet_host_create (NULL ,
1 ,
2 ,
57600 / 8 ,
14400 / 8 );

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

//address.host = 2130706433;
address.port = 1234;
enet_address_set_host (&address, "192.168.0.1");
enet_host_connect(client,&address,2,0);

int time = Millisecs();
while (true)
{
while (enet_host_service (client, & event, 0) > 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 = "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);


enet_packet_destroy (event.packet);

break;

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

event.peer -> data = NULL;
}
}
if (Millisecs()-time>10000) break;
}
Print("Nothing happened, I give up.");
Delay(3000);
return 0;
}

Share this post


Link to post
Share on other sites
192.168.0.1 looks suspicious because routers are setup by default to either 192.168.1.1, or 192.168.2.1 ( i have one of these.. pissed me off). So, are you sure you are trying to connect to the right address? So, the information you provided tells me that you changed your routers internal ip, and also changed its range of given ip addresses.

Share this post


Link to post
Share on other sites
Yeah, I switched that ip with "localhost" and it fixed the error. Still doesn't explain why no connection is being made.

Share this post


Link to post
Share on other sites

This topic is 2779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!