I am not sure if this will help or not its a client and server user register this was not made by me was posted on forums.
Rem Project: Dark Basic Pro Project
Rem Created: Sunday, December 04, 2011
Rem ***** Main Source File *****
` Constants -----------------------------------
#constant MAXIMUM_CLIENTS 16
#constant MAXIMUM_TEXTLINE_AMOUNT 30
#constant MESSAGE_ID_FAIL 0
#constant MESSAGE_ID_SUCCESS 1
#constant MESSAGE_ID_REGISTER 0
#constant MESSAGE_ID_LOGIN 1
#constant MESSAGE_ID_CHAT 2
#constant MESSAGE_ID_NAMES 3
#constant MESSAGE_ID_NEWCLIENT 4
#constant MESSAGE_ID_CLIENTLEFT 5
` ---------------------------------------------
` Variables -----------------------------------
dim TextLines(MAXIMUM_TEXTLINE_AMOUNT) as string
global CURRENT_TEXTLINE_AMOUNT as integer
global CURRENT_TEXT_ENTRY as string
global KEYDOWN_RETURN as boolean
` ---------------------------------------------
sync off
` Obtain server's IP address
input "Please enter the server's IP address: ", ip$
if ip$ = "" then ip$ = "127.0.0.1"
` Connect to server
result = net connect(ip$)
if not result
print "Could not connect to server: "+chr$(34)+net get error()+chr$(34)+"."
wait key
end
endif
print "Connected to server @ "+get time$()+", "+get date$()+"."
` MAIN MENU SECTION
section_menu:
print "1. Login."
print "2. Register"
input "Please choose an option: ", option
if option = 1 then goto section_login
if option = 2 then goto section_register
goto section_menu
` REGISTER SECTION
section_register:
print "--- REGISTER ---"
print "Please enter your desired account details below."
input "Username: ", username$
input "Password: ", password$
net put byte MESSAGE_ID_REGISTER
net put string username$
net put string password$
net send
print "Registering..."
repeat
until net get message()
msgID = net get byte()
if msgID = MESSAGE_ID_SUCCESS
print "Registered as ["+username$+"] with password ["+password$+"]"
else
print "Failed to register ("+net get string()+")"
goto section_register
endif
` LOGIN SECTION
section_login:
print "--- LOGIN ---"
print "Please enter your account details below."
input "Username: ", username$
input "Password: ", password$
net put byte MESSAGE_ID_LOGIN
net put string username$
net put string password$
net send
print "Logging in..."
repeat
until net get message()
msgID = net get byte()
if msgID = MESSAGE_ID_SUCCESS
loggedIn = 1
else
print "Failed to log in. Remember that passwords are case sensitive."
goto section_login
endif
` Initialize stuff
cls
clear entry buffer
DrawTextLines()
DrawEntry()
AddTextLine("Successfully logged in.")
AddTextLine("-----------------------")
set window title "Client - Logged in as "+username$
do
` Check for new messages
while net get message()
ProcessMessage()
endwhile
` Handle user input
HandleInput()
` Pause for 10ms to lower CPU usage
SleepApplication(10)
` Refresh the display
rem sync
loop
function HandleInput()
` Get new text input
newData$ = entry$()
if newData$ <> ""
CURRENT_TEXT_ENTRY = CURRENT_TEXT_ENTRY + newData$
clear entry buffer
` Process backspace
lenHigh = len(CURRENT_TEXT_ENTRY)
lenLow = lenHigh - len(newData$)
for x = lenHigh to lenLow step -1
char = asc(mid$(CURRENT_TEXT_ENTRY, x))
if char = 8
CURRENT_TEXT_ENTRY = left$(CURRENT_TEXT_ENTRY, x-2) + right$(CURRENT_TEXT_ENTRY, len(CURRENT_TEXT_ENTRY)-x)
else
if char < 32
CURRENT_TEXT_ENTRY = left$(CURRENT_TEXT_ENTRY, x-1) + right$(CURRENT_TEXT_ENTRY, len(CURRENT_TEXT_ENTRY)-x)
endif
endif
next x
DrawEntry()
endif
if returnkey()
if not KEYDOWN_RETURN
if CURRENT_TEXT_ENTRY <> ""
` Send chat message
net put byte MESSAGE_ID_CHAT
net put string CURRENT_TEXT_ENTRY
net send
CURRENT_TEXT_ENTRY = ""
DrawEntry()
endif
KEYDOWN_RETURN = 1
endif
else
if KEYDOWN_RETURN
KEYDOWN_RETURN = 0
endif
endif
endfunction
function ProcessMessage()
msgID = net get byte()
select msgID
case MESSAGE_ID_CHAT
username$ = net get string()
message$ = net get string()
AddTextLine("<"+username$+"> "+message$)
endcase
case MESSAGE_ID_NAMES
while net get message remainder() > 0
if names$ <> "" then names$ = names$ + ", "
names$ = names$ + net get string()
endwhile
AddTextLine("Names list: "+names$+".")
endcase
case MESSAGE_ID_NEWCLIENT
AddTextLine(net get string()+" has joined.")
endcase
case MESSAGE_ID_CLIENTLEFT
AddTextLine(net get string()+" has left.")
endcase
endselect
endfunction
function AddTextLine(textLine as string)
` If message buffer full, scroll
if CURRENT_TEXTLINE_AMOUNT = MAXIMUM_TEXTLINE_AMOUNT
TextLines(CURRENT_TEXTLINE_AMOUNT) = textLine
for x = 1 to MAXIMUM_TEXTLINE_AMOUNT
TextLines(x-1) = TextLines(x)
next x
TextLines(MAXIMUM_TEXTLINE_AMOUNT) = ""
DrawTextLines()
else
TextLines(CURRENT_TEXTLINE_AMOUNT) = textLine
inc CURRENT_TEXTLINE_AMOUNT
text 0, (CURRENT_TEXTLINE_AMOUNT-1)*13, TextLines((CURRENT_TEXTLINE_AMOUNT-1))
endif
endfunction
function DrawTextLines()
ink 0, 0
box 0, 0, screen width(), MAXIMUM_TEXTLINE_AMOUNT*13
ink rgb(255, 255, 255), 0
for x = 0 to CURRENT_TEXTLINE_AMOUNT
text 0, x*13, TextLines(x)
next x
line 0, (MAXIMUM_TEXTLINE_AMOUNT+1)*13, 640, (MAXIMUM_TEXTLINE_AMOUNT+1)*13
endfunction
function DrawEntry()
ink rgb(100, 100, 100), 0
box 0, 460, 640, 480
ink rgb(255, 255, 255), 0
text 0, 463, "> "+CURRENT_TEXT_ENTRY
endfunction
function SleepApplication(pauseTime as integer)
if not dll exist(255) then load dll "kernel32.dll", 255
call dll 255, "Sleep", pauseTime
endfunction
Server
Rem Project: Dark Basic Pro Project
Rem Created: Sunday, December 04, 2011
Rem ***** Main Source File *****
Server:
` Constants -----------------------------------
#constant MAXIMUM_CLIENTS 16
#constant MAXIMUM_TEXTLINE_AMOUNT 30
#constant MESSAGE_ID_FAIL 0
#constant MESSAGE_ID_SUCCESS 1
#constant MESSAGE_ID_REGISTER 0
#constant MESSAGE_ID_LOGIN 1
#constant MESSAGE_ID_CHAT 2
#constant MESSAGE_ID_NAMES 3
#constant MESSAGE_ID_NEWCLIENT 4
#constant MESSAGE_ID_CLIENTLEFT 5
` ---------------------------------------------
` Types ---------------------------------------
type ClientData
loggedIn as boolean
username as string
endtype
` ---------------------------------------------
` Variables -----------------------------------
dim Clients(MAXIMUM_CLIENTS) as ClientData
dim TextLines(MAXIMUM_TEXTLINE_AMOUNT) as string
global CURRENT_TEXTLINE_AMOUNT as integer
` ---------------------------------------------
sync off
` Start the server
result = net host(MAXIMUM_CLIENTS)
if not result
AddTextLine("Could not start server: "+chr$(34)+net get error()+chr$(34)+".")
wait key
end
endif
AddTextLine("Server started @ "+get time$()+", "+get date$()+".")
` Initialize stuff
DrawTextLines()
do
` Check for new clients
HandleNewConnections()
` Check for clients disconnecting
HandleNewDisconnections()
` Check for new messages
while net get message()
ProcessMessage()
endwhile
` Pause for 10ms to lower CPU usage
SleepApplication(10)
loop
function HandleNewConnections()
newClient = net player joined()
if newClient > 0
AddTextLine("Client "+str$(newClient)+" has connected.")
endif
endfunction
function HandleNewDisconnections()
leftClient = net player left()
if leftClient > 0
` If client was logged in, display their name, else their number
if Clients(leftClient).loggedIn
AddTextLine(Clients(leftClient).username+" has disconnected.")
Clients(leftClient).loggedIn = 0
DrawNames()
` Inform other clients of this one leaving
net put byte MESSAGE_ID_CLIENTLEFT
net put string Clients(leftClient).username
net send all
else
AddTextLine("Client "+str$(leftClient)+" has disconnected.")
endif
endif
endfunction
function ProcessMessage()
sender = net message from()
msgID = net get byte()
select msgID
case MESSAGE_ID_REGISTER
username$ = net get string()
password$ = net get string()
` Check if account already exists
if file exist("users\"+username$+".txt")
net put byte MESSAGE_ID_FAIL
net put string "username already taken"
net send sender
AddTextLine("Client "+str$(sender)+" failed to register.")
endif
` Create account file
open to write 1, "users\"+lower$(username$)+".txt"
write string 1, username$
write string 1, password$
close file 1
` Send success message
net put byte MESSAGE_ID_SUCCESS
net send sender
endcase
case MESSAGE_ID_LOGIN:
username$ = net get string()
password$= net get string()
` Try to log client in
result = ClientLogin(sender, username$, password$)
if result
net put byte MESSAGE_ID_SUCCESS
net send sender
AddTextLine("Client "+str$(sender)+" successfully logged in as "+Clients(sender).username+".")
` Send list of clients connected
net put byte MESSAGE_ID_NAMES
for x = 1 to MAXIMUM_CLIENTS
if Clients(x).loggedIn
net put string Clients(x).username
endif
next x
net send sender
` Inform other clients of this new one
net put byte MESSAGE_ID_NEWCLIENT
net put string Clients(sender).username
SendAllExcluding1(sender)
` Update our view of the logged in clients
DrawNames()
else
net put byte MESSAGE_ID_FAIL
net send sender
AddTextLine("Client "+str$(sender)+" failed to log in.")
endif
endcase
case MESSAGE_ID_CHAT:
message$ = net get string()
net put byte MESSAGE_ID_CHAT
net put string Clients(sender).username
net put string message$
net send all
AddTextLine("<"+Clients(sender).username+"> "+message$)
endcase
case default:
AddTextLine("Received unknown message of ID"+str$(msgID)+".")
endcase
endselect
endfunction
function ClientLogin(clientNumber as integer, username as string, password as string)
if not file exist("users\"+username+".txt") then exitfunction 0
open to read 1, "users\"+username+".txt"
read string 1, tempUsername$
read string 1, tempPassword$
close file 1
if tempPassword$ <> password then exitfunction 0
` Check that client isn't already logged in
for x = 1 to MAXIMUM_CLIENTS
if Clients(x).loggedIn
if Clients(x).username = tempUsername$ then exitfunction 0
endif
next x
Clients(clientNumber).username = tempUsername$
Clients(clientNumber).loggedIn = 1
endfunction 1
function SendAllExcluding1(exclusion as integer)
for x = 1 to MAXIMUM_CLIENTS
if Clients(x).loggedIn
if x <> exclusion
net send x, 1
endif
endif
next x
net send 0
endfunction
function AddTextLine(textLine as string)
` If message buffer full, scroll
if CURRENT_TEXTLINE_AMOUNT = MAXIMUM_TEXTLINE_AMOUNT
TextLines(CURRENT_TEXTLINE_AMOUNT) = textLine
for x = 1 to MAXIMUM_TEXTLINE_AMOUNT
TextLines(x-1) = TextLines(x)
next x
TextLines(MAXIMUM_TEXTLINE_AMOUNT) = ""
DrawTextLines()
else
TextLines(CURRENT_TEXTLINE_AMOUNT) = textLine
inc CURRENT_TEXTLINE_AMOUNT
text 0, (CURRENT_TEXTLINE_AMOUNT-1)*13, TextLines((CURRENT_TEXTLINE_AMOUNT-1))
endif
endfunction
function DrawTextLines()
cls
for x = 0 to CURRENT_TEXTLINE_AMOUNT
text 0, x*13, TextLines(x)
next x
line 0, (MAXIMUM_TEXTLINE_AMOUNT+1)*13, 640, (MAXIMUM_TEXTLINE_AMOUNT+1)*13
endfunction
function DrawNames()
ink 0, 0
box 0, (MAXIMUM_TEXTLINE_AMOUNT+2)*13, screen width(), (MAXIMUM_TEXTLINE_AMOUNT+2)*13+15
ink rgb(255, 255, 255), 0
for x = 1 to MAXIMUM_CLIENTS
if Clients(x).loggedIn then names$ = names$ + Clients(x).username + " "
next x
text 0, (MAXIMUM_TEXTLINE_AMOUNT+2)*13, names$
endfunction
function SleepApplication(pauseTime as integer)
if not dll exist(255) then load dll "kernel32.dll", 255
call dll 255, "Sleep", pauseTime
endfunction
======================================
My software never has bugs. It just develops random features.