Jump to content

  • Log In with Google      Sign In   
  • Create Account


[Resolved] Simple enet program fails


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • This topic is locked This topic is locked
17 replies to this topic

#1 Josh Klint   Crossbones+   -  Reputation: 1349

Like
0Likes
Like

Posted 30 April 2011 - 02:31 PM

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++.

Build mobile games with native code

http://www.leadwerks.com


Sponsor:

#2 smasherprog   Members   -  Reputation: 432

Like
0Likes
Like

Posted 30 April 2011 - 04:33 PM

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;
	}	
}

Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

#3 Josh Klint   Crossbones+   -  Reputation: 1349

Like
0Likes
Like

Posted 30 April 2011 - 06:48 PM

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

Build mobile games with native code

http://www.leadwerks.com


#4 Josh Klint   Crossbones+   -  Reputation: 1349

Like
0Likes
Like

Posted 30 April 2011 - 07:18 PM

:o

I wonder if the two languages are using two different versions of enet that can't communicate. :blink:

Build mobile games with native code

http://www.leadwerks.com


#5 Josh Klint   Crossbones+   -  Reputation: 1349

Like
0Likes
Like

Posted 30 April 2011 - 07:52 PM

I was using two different incompatible versions of enet, but after fixing that problem, behavior is unchanged...

Build mobile games with native code

http://www.leadwerks.com


#6 smasherprog   Members   -  Reputation: 432

Like
0Likes
Like

Posted 30 April 2011 - 08:05 PM

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.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

#7 Josh Klint   Crossbones+   -  Reputation: 1349

Like
0Likes
Like

Posted 01 May 2011 - 07:10 PM

Strange things happening...

Build mobile games with native code

http://www.leadwerks.com


#8 Josh Klint   Crossbones+   -  Reputation: 1349

Like
0Likes
Like

Posted 01 May 2011 - 08:32 PM

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


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;
}

Build mobile games with native code

http://www.leadwerks.com


#9 smasherprog   Members   -  Reputation: 432

Like
1Likes
Like

Posted 01 May 2011 - 10:06 PM

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.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

#10 Josh Klint   Crossbones+   -  Reputation: 1349

Like
0Likes
Like

Posted 01 May 2011 - 10:27 PM

Yeah, I switched that ip with "localhost" and it fixed the error. Still doesn't explain why no connection is being made.

Build mobile games with native code

http://www.leadwerks.com


#11 Josh Klint   Crossbones+   -  Reputation: 1349

Like
0Likes
Like

Posted 01 May 2011 - 10:56 PM

Even more interesting: I removed Lua and Luabind, so I could package up the project and post it here, and now the connection magically works...

Build mobile games with native code

http://www.leadwerks.com


#12 smasherprog   Members   -  Reputation: 432

Like
1Likes
Like

Posted 01 May 2011 - 11:12 PM

Because of the fact that the connection works when you specify your local lost (127.0.0.1), I really think that you are trying to connect to the wrong address. An address of "192.168.0.1" would be reserved for the address of your router. You should be trying to connect to an address like, 192.168.0.2. Or, if you were on a standard setup, 192.168.1.2, or 192.168.1.3 .... etc
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

#13 SymLinked   Members   -  Reputation: 845

Like
0Likes
Like

Posted 02 May 2011 - 04:47 AM

Even more interesting: I removed Lua and Luabind, so I could package up the project and post it here, and now the connection magically works...


Ouch. Are you sure you have made a full recompile of the project? If you insert those libraries again, is the problem reproduced? Can you ping directly via your O/S to the address you are trying to connect to?

Contrary to what the previous poster says, there's nothing wrong with having a zero as the third octet in an IPv4 address. For instance, this is the default on some Netgear routers.
They also do not have to (but usually do in typical setups) reside on the first IP in the range. There's nothing non-standard about this and it likely has nothing to do with your problem.

#14 Josh Klint   Crossbones+   -  Reputation: 1349

Like
0Likes
Like

Posted 02 May 2011 - 09:22 AM

The IP address thing is resolved. I just haven't done any network programming for a while, so I forgot the default local address.

I'll do some more testing and figure out exactly what I am including that messes up enet.

Build mobile games with native code

http://www.leadwerks.com


#15 Josh Klint   Crossbones+   -  Reputation: 1349

Like
0Likes
Like

Posted 02 May 2011 - 11:22 AM

Well, it works fine now after I added everything back in. Maybe my change to the IP address somehow wasn't get recompiled.

Build mobile games with native code

http://www.leadwerks.com


#16 smasherprog   Members   -  Reputation: 432

Like
1Likes
Like

Posted 02 May 2011 - 01:07 PM

Josh, that is great, but I am curious, you are saying it works using the 192.168.0.1 address? Sorry, I just have to know.

Oh and to SymLinked, thanks for voting down all of my posts, hopefully the moderators can rectify it.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

#17 Josh Klint   Crossbones+   -  Reputation: 1349

Like
0Likes
Like

Posted 02 May 2011 - 01:52 PM

No, I just use "localhost" and it works.

Build mobile games with native code

http://www.leadwerks.com


#18 hplus0603   Moderators   -  Reputation: 5155

Like
0Likes
Like

Posted 04 May 2011 - 03:19 PM

Josh, that is great, but I am curious, you are saying it works using the 192.168.0.1 address? Sorry, I just have to know.

Oh and to SymLinked, thanks for voting down all of my posts, hopefully the moderators can rectify it.


Please, no discussing personal issues on the board, or I will have to moderate it. Take it to PM if it matters.

Moderators do not get involved in ratings issues, unless there is some bug with the system. Over time, ratings tend to work themselves out.
enum Bool { True, False, FileNotFound };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS