Sign in to follow this  

NAT Punch function on Raknet.

This topic is 4578 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 found AdvertiseSystem in Raknet library.
	// Description:
	// Sends a one byte message ID_ADVERTISE_SYSTEM to the remote unconnected system.
	// This will tell the remote system our external IP outside the LAN, and can be used for NAT punch through
	//
	// host: Either a dotted IP address or a domain name
	// remotePort: Which port to connect to on the remote machine.
	virtual void AdvertiseSystem(char *host, unsigned short remotePort)=0;


so i try this function. i'd like to create a introducer to list all server available. so the server called advertiseSystem function. here is my code to test the function
// the server called the advertise
int main()
{
	RakServerInterface *pRakServer = RakNetworkFactory::GetRakServerInterface();
	pRakServer->Start( 10, 0, 0, 60001 );
	char key;
	while( 1 )
	{
		if( kbhit() )
		{
			key = getch();
			if( key == 'c' )
			{
				pRakServer->AdvertiseSystem( "192.168.0.27", 60000 );
			}
			if( key == 27 )
				break;
		}
	}

	RakNetworkFactory::DestroyRakServerInterface( pRakServer );
	pRakServer = 0;
	return 0;
}

and here is my introducer
// the introducer to list all server, 
// for now i just want to know if the introducer received advertise packet
class ServerTest: public Multiplayer<RakServerInterface>
{
public:
	ServerTest();
	~ServerTest();
	void Update();
private:
	void ProcessUnhandledPacket( Packet *pPacket, unsigned char packetIdentifier, RakServerInterface *pRakServer );
	void ReceiveAdvertisedSystem( Packet *pPacket, RakServerInterface *pRakServer );
private:
	RakServerInterface *m_pRakServer;
};

ServerTest::ServerTest()
{
	m_pRakServer = RakNetworkFactory::GetRakServerInterface();
	m_pRakServer->Start( 10, 0, 0, 60000 );
}

ServerTest::~ServerTest()
{
	m_pRakServer->Disconnect( 3000 );
	RakNetworkFactory::DestroyRakServerInterface( m_pRakServer );
	m_pRakServer = 0;
}

void ServerTest::Update()
{
	ProcessPackets( m_pRakServer );
}

void ServerTest::ProcessUnhandledPacket( Packet *pPacket, unsigned char packetIdentifier, RakServerInterface *pRakServer )
{
	
}

void ServerTest::ReceiveAdvertisedSystem( Packet *pPacket, RakServerInterface *pRakServer )
{
	printf( "Get an advertisedSystem from address:%u port%u", 
		pPacket->playerId.binaryAddress, pPacket->playerId.port );
}

int main()
{
	ServerTest myServer;
	while( !kbhit() )
	{
		myServer.Update();
	}
	return 0;
}

it did receive the advertise packet, but strange result occur. the server use port 60001, and introducer use port 60000 but the result of this, is the introducer received port 52685 in pPacket->playerId. so i can't introduce this server. why this is happening? shouldn't the introducer receive port 60001 instead? thx..

Share this post


Link to post
Share on other sites
52685 is 0xCDCD.

That sounds to me like uninitialised value (memory cleared possibly with 0xCDCDCDCD).

You sure you pick up the right packet type?

Just a wild shot in the dark.

Share this post


Link to post
Share on other sites
it should be right packet.
because i'm using function from raknet.
and the introducer does received ID_ADVERTISE_SYSTEM, so introducer's ReceiveAdvertisedSystem called. everything seems right but the port.
i don't undestand why the port is uninitialised, if advertiseSystem is used for nat punch, shouldn't it receive the server port??

thx for answering..

Share this post


Link to post
Share on other sites
i've read FAQ about the NAT punch, and i got many answer about my problem.
now, what i'm asking is NAT punch using raknet.
from what i read, for NAT punch, i should get the global ip and port, and when i search in RakNet library i found about advertiseSystem, and it says it can be used for NAT punch.
and i tried the function and it seems to work, but the only strange think happenning is i don't get the port.
Can I do NAT punch, without the port??

Share this post


Link to post
Share on other sites
If you want to go peer to peer, you have to get address and port.

The source to RakNet is available AFAIR, so you could just trace into it and figure out how to get what you need. For more help with RakNet internals, I'd suggest the RakNet formus at rakkarsoft.

Share this post


Link to post
Share on other sites

This topic is 4578 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this