Hello Again
I have finally reached the last chapter of The Beginner's Guide to DarkBASIC and the main focus of the chapter is networking and multi-player. I have finished two exercise codes but each time I run them, a problem occurs with the Client side. If I choose client, I am asked to input the host's IP address. However once I do, and it starts to connect to the host, the game stops and an error message like this pops up. "ChatClient.exe has encountered a problem and needs to close. We are sorry for the inconvenience." If someone could tell me what the problem is and then suggest some steps to fix it, that would be great.
Rem Project: ChatClient
Rem Created: Wednesday, May 02, 2012
Rem Designer: Andrew D. South
Rem ***** Main Source File *****
SYNC ON
SYNC RATE 30
`Keep track of what was said and who said what
DIM chattext$(32)
DIM playersname$(1)
DIM numberofplayers(1)
DIM lastnumberofplayers(1)
`Find the TCP/IP connection number
tcpipnumber = FINDTCPIPCONNECTIONNUMBER()
PRINT "Simple network chat client!"
PRINT
SYNC
` Get their names
INPUT "Please enter a name: ", myname$
SYNC
SYNC
IF myname$ = ""
PRINT "THAT'S NO NAME!!!"
WAIT KEY
END
ENDIF
`Find out who host and client are..
PRINT "(1) I'm the Host"
PRINT "(2) I'm the Client"
SYNC
SYNC
A$ = ""
answer = 0
`Get Host and Client
WHILE answer = 0
A$ = INKEY$()
IF A$ ="1" THEN answer = 1
IF A$ ="2" THEN answer = 2
ENDWHILE
`Do this if host
IF answer = 1
PRINT "Creating Net Session. Please Wait"
SYNC
SLEEP 200
SET NET CONNECTION tcpipnumber, " "
CREATE NET GAME "Sample Net Session", myname$, 16, 1
ENDIF
`Do this if Client
IF answer = 2
INPUT "Please enter the Host's IP Address: ", addressdate$
PRINT "Connection to net session. Please Wait..."
SYNC
SET NET CONNECTION tcpipnumber, addressdata$
PERFORM CHECKLIST FOR NET SESSIONS
numberofgames =CHECKLIST QUANTITY()
IF numberofgames = 0
PRINT "No session found at that address"
SYNC
WAIT KEY
END
ENDIF
JOIN NET GAME 1, myname$
PRINT "Connected to sessions!"
SYNC
ENDIF
` Do Chat Client
CHATCLIENT()
END
`This function will determing NET CONNECTION number
`if TCP/IP
FUNCTION FINDTCPIPCONNECTIONNUMBER()
Flag = 0
CLS
PERFORM CHECKLIST FOR NET CONNECTIONS
FOR X = 1 TO CHECKLIST QUANTITY()
Service$ = CHECKLIST STRING$ (X)
IF LEFT$(service$,15)="Internet TCP/IP"
Flag = X
ENDIF
NEXT X
ENDFUNCTION Flag
`This function does all the chat client functionality.
FUNCTION CHATCLIENT()
`Clears the chat text from the array
CLEARCHATTEXT()
` Display the initial players in the room.
PERFORM CHECKLIST FOR NET PLAYERS
numberofplayers(1) = CHECKLIST QUANTITY()
FOR X = 1 TO numberofplayers(1)
ADDMESSAGEUSER(CHECKLIST STRING$(X))
NEXT X
` Send a comming message
C$ = playersname$(1)+" has joined."
SEND NET MESSAGE STRING 0,C$
` Displays the chat text..
DISPLAYCHATTEXT()
` Set the entry buffers
A$ = ""
B$ = ""
C$ = ""
CLEAR ENTRY BUFFER
` Capture Text Input and process it accordingly
WHILE ESCAPEKEY() = 0
CHECKINCOMINGMESSAGES()
A$ = INKEY$()
IF ASC(A$) = 8
C$ = C$ + ENTRY$()
C$ = LEFT$(C$,LEN(C$)-1)
CLEAR ENTRY BUFFER
CLS
DISPLAYCHATTEXT()
ENDIF
IF RETURNKEY()=1 AND B$ <> ""
SLEEP 250
` Send Remote Message
D$ = playersname$(1)+": "+B$
SEND NET MESSAGE STRING 0,D$
` Display local mesage
ADDSTRINGTOCHAT(D$)
D$ = ""
B$ = ""
C$ = ""
CLEAR ENTRY BUFFER
` Display New Chat Window
DISPLAYCHATTEXT()
ENDIF
SYNC
ENDWHILE
ENDFUNCTION
`Scans the incoming messages for strings
`and displays them.
FUNCTION CHECKINCOMINGMESSAGES()
GET NET MESSAGE
IF NET MESSAGE EXISTS()=0 THEN EXITFUNCTION
WHILE NET MESSAGE EXISTS()<>0
msgtype = NET MESSAGE TYPE()
IF msgtyp = 3
msg$ = NET MESSAGE STRING$()
ADDSTRINGTOCHAT(msg$)
DISPLAYCHATTEXT()
ENDIF
GET NET MESSAGE
ENDWHILE
ENDFUNCTION
`Message to display if a User has joined
FUNCTION ADDMESSAGEUSER(name$)
newstring$ = name$+" is here."
ADDSTRINGTOCHAT(newstring$)
ENDFUNCTION
`Adds a string to the ChatText$ Array
FUNCTION ADDSTRINGTOCHAT(A$)
FOR X = 1 TO 32
IF chattext$(X) = ""
chattext$(X) = A$
EXITFUNCTION
ENDIF
NEXT X
FOR X = 32 TO 2
Y = X - 1
chattext$(Y) = chattext$(X)
NEXT X
chattext$(32) = A$
ENDFUNCTION
` Clears the ChatText$ Variables
FUNCTION CLEARCHATTEXT()
FOR X = 1 TO 32
chattext$(X) = ""
NEXT X
ENDFUNCTION
`Displays the Chat text on screen
FUNCTION DISPLAYCHATTEXT()
CLS
SET CURRENT BITMAP 0
CENTER TEXT 320,10,"Chat Client"
FOR X = 1 TO 32
TEXT 10,10+(X*15),chattext$(X)
NEXT X
ENDFUNCTION
When we all lend our power together, there is nothing we
can't do!
Please Lend me your STRENGTH!