Internet GNet Connection

BlitzMax Forums/BlitzMax Programming/Internet GNet Connection

Ok, I'm starting to feel the pangs of intense frustration setting in. I've successully been testing my game using Gnet by connecting to 127.0.0.1 but when I attempt to connect to someone else on the internet I don't get any kind of connection. I've also tried connecting to myself using my non-local ip address which doesn't work.

I'm using mark's gnetchat.bmx as a base to attempt this but everything I do leads me to the inexorable conclusion that it doesn't work.

I've tried shutting off my windows firewall - doesn't work
I've tried opening a port in the firewall - doesn't work
I've tried allowing firewall exception access to the gnetchat.debug.exe file - doesn't work
I've tried opening the port on my router - doesn't work
I've tried setting my router to DMZ - doesn't work

Am I missing something? Has anyone got this working successully? Is it my setup? Am I protecting my pc so much that I might make it impossible to handle multiplayer games?

* run the chat and listen
* run another chat and connect to the remote IP it states.
* no connection

Any help would be greatly appreciated.

See below for Mark's code:

Matt

Strict

AppTitle="GNet Test2"

Local host:TGNetHost=CreateGNetHost()

Local me:TGNetObject
Local chat$,info$

Graphics 520,200,0,15

Local ip$=GetIP()

Repeat

	Local c=GetChar()
	
	Select c
	Case 8
		If chat chat=chat[..chat.length-1]
	Case 27
		'If Confirm( "Quit?" )
			CloseGNetHost host
			End
		'EndIf
	Case 13
		If chat.find("/")=0
			chat=chat[1..]
			Local cmd$=chat
			Local arg$
			Local i=chat.find(" ")
			If i<>-1
				cmd=chat[..i]
				arg=chat[i+1..]
			EndIf
			Select cmd
			Case "create"
				If me
					info="Already created"
				Else
					me=CreateGNetObject( host )
					SetGNetString me,0,arg
					SetGNetString me,1,"Ready"
				EndIf
			Case "close"
				If me
					CloseGNetObject me
					me=Null
				Else
					info="Not created"
				EndIf
			Case "quit","exit"
				CloseGNetHost host
				End
			Case "nick"
				If arg
					If me SetGNetString me,0,arg
					info="Nick changed to "+arg
				Else
					info="Expecting arg"
				EndIf
			Case "listen"
				Local port=17263
				If arg port=Int(arg)
				If GNetListen( host,port )
					info="Listening on port "+port+" / "+ip
				Else
					info="Listen failed"
				EndIf
			Case "connect"
				If arg
					Local addr$=arg
					Local port=17263
					Local i=arg.find(":")
					If i<>-1
						addr=arg[..i]
						port=Int(arg[i+1..])
					EndIf
					If GNetConnect( host,addr,port )
						info="Connected to "+addr+":"+port
					Else
						info="Failed to connect to "+addr+":"+port
					EndIf
				Else
					info="Expecting arg"
				EndIf
			Default
				info="Unrecognized command '"+cmd+"'"
			End Select
		Else
			If me SetGNetString me,1,chat
		EndIf
		chat=""
	Default
		If c>31 And c<127 chat:+Chr(c)
	End Select
	
	GNetSync host
	
	Cls

	Local y,h=GraphicsHeight()
	
	For Local obj:TGNetObject=EachIn GNetObjects( host,GNET_ALL )
		If obj.state()=GNET_CLOSED Continue
		If obj=me
			SetColor 255,255,255
		Else
			SetColor 0,128,255
		EndIf
		DrawText GetGNetString( obj,0 )+":"+GetGNetString( obj,1 ),0,y
		y:+16
	Next
	
	SetColor 255,0,0
'	Local cons:TList=GNetPeers( host )
	
	SetColor 255,255,0
	DrawText info,0,h-32
	
	SetColor 0,255,0
	DrawText ">"+chat,0,h-16
	DrawRect TextWidth(">"+chat),h-16,8,16
	DrawText "/create nick    /listen    /connect host    /quit    /nick newnick",0,h-48
	
	Flip
	
Forever


Function getIP$()
		Local ip$=""
		'get internet ip address
		'*** you will need to specify your own URl here & change the text search below ***
		Local ipStream:TStream = ReadStream("http::www.whatismyip.com/automation/n09230945.asp") 
	
		If ipStream
			ip$ =ReadLine(ipStream)
		EndIf
		
		CloseStream ipStream
		Return ip
End Function



the one that is hosting the server must have portforwarding enabled, it helps nothing if you set the stuff at your end and connect to him as his router / firewall will just drop you.

works fine for me, are you using your real ip or the one your router gives you?

goto http://www.whatismyip.com/ to get your ip, it atleast worked for me using the ip it gives me. hope that helps

EDIT:
just reread your post and it sounds like you've already done that. for me and my game that i'm working on my friend can connect to me using the ip that that website gives me.

heres some test code i typed up its not much and its not perfect, but it works.


Import BRL.GNet
AppTitle="multitest"
Graphics 640,480
SeedRnd MilliSecs()

'gfx
Global character = LoadImage("character.png")

'var
Global p1x=Rand(0,575),p1y=Rand(0,416)
Global p2x,p2y
Global oldx,oldy
Global numplayers = 1
Global multiOn = 0
Global myStatus$ = "not connected"
Global connected = 0
Global host:TGNetHost = CreateGNetHost()
Global port = 12345
Global ip$ = "127.0.0.1"
Global timeout_ms = 10000 
Global hosting

Global localObj:TGNetObject = CreateGNetObject:TGNetObject( host )
Global remoteObj:TGNetObject
Global objList:TList = New TList 

Const SLOT_CONNECT = 0
Const SLOT_X = 1
Const SLOT_Y = 2

While Not AppTerminate()
Cls
	If multiOn = 0 Or multiOn = 2
		If numplayers = 2
			'player2
			SetColor 255,0,0
			DrawImage character,p2x,p2y
		EndIf
		
		'player1
		SetColor 255,255,255
		DrawImage character,p1x,p1y		
	EndIf	
		
	Keys()		
	
	If connected = 1
		SendInfo()	
		GNetSync host
		ReceiveInfo()		
	EndIf	
			
	If multiOn = 1
		DrawText "[H]ost or [Join]",200,200		
	EndIf
	
SetColor 255,255,255

'debug info
DrawText "p1x: "+p1x,10,50
DrawText "p1y: "+p1y,10,60
DrawText "oldx: "+oldx,10,70
DrawText "oldy: "+oldy,10,80
DrawText "p2x: "+p2x,10,100
DrawText "p2y: "+p2y,10,110

'connection status
DrawText "Players: "+numplayers,10,10
DrawText "Status: "+myStatus$,10,440
If multiOn = 0
	DrawText"M - Start Multiplayer.   Arrow Keys - Move Player.",10,460
Else
	DrawText"Arrow Keys - Move Player.",10,460
EndIf
Flip
Wend
End

Function ConnectTo()
	CloseGNetHost(host)
	host:TGNetHost=CreateGNetHost()
	localObj = CreateGNetObject:TGNetObject( host )

	If hosting = 1
		success = GNetListen( host, port )
		If Not success
			connected = 0
			myStatus$ = "GNetListen failed"			
			multiOn = 0
		Else
			connected = 1			
			myStatus$ = "connected -- HOST"			
			SetGNetInt(localObj, SLOT_CONNECT,1)
			SendInfo()
			multiOn = 2			
		EndIf
	Else
		success = GNetConnect( host, ip$, port, 10000 )
		If Not success
			connected = 0
			myStatus$ = "GNetConnect failed"
			multiOn = 0
		Else
			connected = 1
			myStatus$ = "connected -- CLIENT"
			SetGNetInt(localObj, SLOT_CONNECT,1)
			SendInfo()
			multiOn = 2
		EndIf
	EndIf		
EndFunction

Function Keys()
	If multiOn = 0  Or multiOn = 2
		If KeyDown(key_Left)
			p1x:-10
			If p1x <0 Then p1x = 0
		EndIf
		If KeyDown(key_Right)
			p1x:+10
			If p1x > 575 Then p1x = 575
		EndIf		
		If KeyDown(key_Up)
			p1y:-10
			If p1y <0 Then p1y = 0
		EndIf
		If KeyDown(key_down)
			p1y:+10
			If p1y > 416 Then p1y = 416
		EndIf
		
		If multiOn = 0
			If KeyDown(key_M)
				multiOn = 1
			EndIf
		EndIf
	EndIf
	
	If multiOn = 1
		If KeyDown(key_H)
			hosting = 1
			ConnectTo()
		EndIf
		If KeyDown(key_J)
			hosting = 2
			EnterIP()
		EndIf
	EndIf
	
	If KeyDown(key_Escape) Then End
EndFunction

Function EnterIP()
Local done = 0

	Repeat
	Cls
		DrawText"Enter IP:> "+ip$+"*",200,200
		
		c=GetChar()
		If c
			If c = 8
				If Len(ip$)>0 Then ip$=Left$(ip$,Len(ip$)-1)
			EndIf
			Select Chr(c)
				Case 1
					ip$ = ip$ +Chr(c)
				Case 2
					ip$ = ip$ +Chr(c)
				Case 3
					ip$ = ip$ +Chr(c)
				Case 4
					ip$ = ip$ +Chr(c)
				Case 5
					ip$ = ip$ +Chr(c)
				Case 6
					ip$ = ip$ +Chr(c)
				Case 7
					ip$ = ip$ +Chr(c)
				Case 8
					ip$ = ip$ +Chr(c)
				Case 9
					ip$ = ip$ +Chr(c)
				Case 0
					ip$ = ip$ +Chr(c)
				Case "."
					ip$ = ip$ +Chr(c)
			EndSelect
		EndIf
		
		If KeyHit(key_Enter)			
			done = 1
			Cls
			DrawText"attempting to connect to ip: "+ip$,200,200
		EndIf
	Flip
	Until done = 1
	
	ConnectTo()
EndFunction

Function SendInfo()
	If p1x <> oldx
		oldx = p1x
		SetGNetInt localObj,SLOT_X,p1x
	EndIf
	If p1y <> oldy
		oldy = p1y
		SetGNetInt localObj,SLOT_Y,p1y
	EndIf	
EndFunction

Function ReceiveInfo()
numplayers = 0
objList = GNetObjects( host, GNET_ALL )
	For remoteObj = EachIn objList
		'Local c = GetGNetInt(remoteObj,SLOT_CONNECT)
		'If c = 1 Then numplayers:+1
		numplayers:+GetGNetInt(remoteObj,SLOT_CONNECT)
	Next
	
	objList = GNetObjects( host, GNET_MODIFIED )
	For remoteObj = EachIn objList
		'Local c = GetGNetInt(remoteObj,SLOT_CONNECT)
		'If c = 1 Then numplayers:+1
		'numplayers:+GetGNetInt(remoteObj,SLOT_CONNECT)
		
		p2x = GetGNetInt(remoteObj, SLOT_X)
		p2y = GetGNetInt(remoteObj, SLOT_Y)
	Next	
EndFunction



This is where it gets frustrating. I have forwarded the port on the router and can't think of anything that might help fix this problem. Even *disabling* firewalls doesn't work!

What firewall are you using King? The windows firewall and he router firewall?

im using both windows firewall and sygate, sometimes i disable them and sometimes i don't for testing. when i tested your code i didn't disable anything the usual "blah trying blah connect blah...." warnings came up, i allowed and it woked fine. on my router i've tested changing the dmz and not changing before and either way seems to work for me.

EDIT:
i've gotta addmit though, i don't know/understand much about networking and routers ( port forwarding and all that). i just try things and they either work or don't, if they don't i keep trying untill i find a working solution.

Well thanks for your help, King. I'm about to give up. I've tried for two days and I've had no luck.

Can you ping the IP you want to connect to?
This would be a low level test.

Yeah, I can ping it and it returns within one hop and under 1ms. I can also ping my local IP, 192.168.1.4 and it also returns within one hop.

The port is open.
the firewall is off
the server is listening on the assigned port
but I can't connect to the IP.

It's really quite, quite annoying.

I see a lot of demo apps, especially with chat. But has anybody built a multiplayer game / example App with GNet or RakNet they would be willing to share?