Here is the code that will let you set up a TCP/IP net game and not have to worry about other people needing the IP or having to find it. The code itself is incomplete, its just the core or what you need to get the system working, you’ll need to added your own code to the top and bottom of each section. I am also providing a slightly change version of a script IanM created, however without this system wouldn’t work.
First things first, the host script is the easiest of the two scripts to setup, the first things the script does it gets the computer IP with that none of this will work. The IP it then stores in a text file along with the game name, the player name and the number of players, the file is then named
gamename$ + “.game”. Once this file is created we then upload the file to an ftp director that contains all of the net games. The ftp directory should be checked for a game with the same name before you upload the file.
Now you know how to do that bit here the code for it:
Rem host code
ip$ = getip()
open to write 1,gamename$ + ".game"
write string 1,gamename$
write string 1,playername$
write string 1,ip$
write string 1,"8"
close file 1
ftp connect url$, username$, password$
ftp set dir directory$
ftp find first
ftp find next
for t = 1 to 20
ftp find next
if get ftp file name$() = gamename$ + ".game"
delete file gamename$ + ".game"
cls
print "Game name already in use"
sync
wait 5000
end
endif
next t
ftp find next
ftp put file gamename$ + ".game"
if get ftp failure() = 1
test$ = get ftp error$()
delete file gamename$ + ".game"
cls
print test$
sync
wait 5000
end
endif
delete file gamename$ + ".game"
ftp disconnect
Next you may have created the game but it useless unless you can connect to it, so first things first you will need to make an array that will contain all the information of the game currently active. Next you want to connect to the ftp directory and download all the .game files one by one. Every time you download a file save it then open the file get each of the 4 bit of data out and save it to the array, then close the file and delete it. Keep doing that until you have all the files then you will need to create a way to display and select the games *this bit I haven’t included in the code as you will want to make your own*. Once the user has select a game you then tell the game to connect to that IP address selected and then connect to the first game.
Now if you look at the example it should make more sense
:
rem join code
dim gamedata$(20,4)
ftp connect url$, username$, password$
ftp set dir directory$
ftp find first
ftp find next
ftp find next
gamename$ = get ftp file name$()
ftp get file gamename$,gamename$
t = 1
nomore = 0
while nomore = 0 and t <= 10
if file exist(gamename$) = 1
open to read 1,gamename$
read string 1,gamedata$(t,1)
read string 1,gamedata$(t,2)
read string 1,gamedata$(t,3)
read string 1,gamedata$(t,4)
close file 1
endif
delete file gamename$
ftp find next
if get ftp file name$() = gamename$
nomore = 1
else
gamename$ = get ftp file name$()
ftp get file gamename$,gamename$
t = t + 1
endif
endwhile
ftp disconnect
rem put some sort of selection screen here that saves the number of the selected game to gamedata$
perform checklist for net connections
for t=1 to checklist quantity()
if checklist string$(t) = "Internet TCP/IP Connection For DirectPlay" then set net connection t,gamedata$(gamenum,3)
next t
join net game 1,playername$
rem put some sort of selection screen here that saves the number of the selected game to gamedata$
perform checklist for net connections
for t=1 to checklist quantity()
if checklist string$(t) = "Internet TCP/IP Connection For DirectPlay" then set net connection t,gamedata$(gamenum,3)
next t
join net game 1,playername$
Now that you can do all of that here is the script that let you get the IP address:
rem these functoins are used to get the ip and were created by IanM
function GetLocalIP(index as integer)
local ip as string
local p as DWORD
local HOSTENT as DWORD
HOSTENT=make memory(256)
if GetHostName( HOSTENT, 256 ) = 0
HostInfo=GetHostByName( HOSTENT )
if HostInfo <> 0
p=HostInfo+12
p=*p
p=*p
p=p+(index*4)
ip=INET_ntoa( *p )
endif
endif
delete memory HOSTENT
endfunction ip
function GetHostName(memptr as DWORD, size as integer)
local r as integer
local a as DWORD
local s as DWORD
r=call dll(201, "gethostname", memptr, size)
endfunction r
function GetHostByName(memptr as DWORD)
local r as integer
r=call dll(201, "gethostbyname", memptr)
endfunction r
function INET_ntoa(addr as DWORD)
local ip as string
ip=call dll(201, "inet_ntoa", addr)
endfunction ip
function WSAStartup()
local WSADATA as DWORD
local r as integer
local v as WORD
load dll "wsock32.dll",201
WSADATA=make memory(16384)
v=2
r=call dll(201, "WSAStartup", v, WSADATA)
delete memory WSADATA
endfunction r
function WSACleanup()
call dll 201, "WSACleanup"
delete dll 201
endfunction
rem gets and stores ip this function is the one you want to use to get the ip
function getip()
if WSAStartup() = 0
yourip$ = GetLocalIP(0)
WSACleanup()
endif
endfunction yourip$
Ok, I’ve only tried this one a few computer and it seems to work a treat, so if you have any problems ask me and I’ll see if I can fix them for you
Once again thanks to IanM for is IP getting code.
Skid