NAT Punch function on Raknet.

Started by
5 comments, last by EkEk 18 years, 10 months ago
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..
Advertisement
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.

Everything is better with Metal.

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..
just wanna say...
that i still not found the answer...
still waiting somebody to help me..
i don't have a clue what i suppose to do for NAT punch
The theory of NAT punch-through is described in the Forum FAQ. And Game Programming Gems 5 :-)
enum Bool { True, False, FileNotFound };
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??
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.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement