Mark
Mark

Reputation: 49

Receiving and Sending Strings over the Network in QB64

I'm working on a mini pokemon game for my brother that works over the network. Unfortunately, when testing I find that for some reason, it gives an error about "Bad file name or number" only on the lines where it tries to send a string to another computer, but has no error when looping the receive command.

Here's my code:

SCREEN 12
CLS
PRINT ""
PRINT ""
PRINT ""
PRINT ""
PRINT ""
PRINT "                       POKELITE - By Mark "
PRINT ""
PRINT ""
INPUT "Join or Host a game? ", hostorjoin$
hostorjoin$ = UCASE$(hostorjoin$)
IF hostorjoin$ = "JOIN" THEN GOTO JOIN
IF hostorjoin$ = "HOST" THEN GOTO HOST

HOST:
server& = _OPENHOST("TCP/IP:300")
PRINT "Waiting for connection..."
PRINT "! Remember: If playing locally, give the other player your IPv4 Address !"
DO
    HOST& = _OPENCONNECTION(server&)
LOOP UNTIL HOST& <> 0
PRINT ""
PRINT "2nd Player Joined!"
SLEEP 2
GOTO GAME
JOIN:
INPUT "Enter Server IPv4 Address (Example: 192.168.1.25): ", joinip$
handle& = _OPENCLIENT("TCP/IP:300:" + joinip$)
IF handle& = 0 THEN PRINT "Connection failed!": SLEEP 2: CLS: GOTO JOIN
GOTO GAME
GAME:
CLS
INPUT "Enter your name: ", name$
IF name$ = "" THEN GOTO GAME
PRINT "Waiting for other player..."
IF hostorjoin$ = "JOIN" THEN
    PUT HOST&, , name$
    DO
        GET handle&, , name2$
    LOOP UNTIL name2$ <> ""
END IF
IF hostorjoin$ = "HOST" THEN
    PUT handle&, , name$
    DO
        GET HOST&, , name2$
    LOOP UNTIL name2$ <> ""
END IF
PRINT name$
PRINT name2$

Upvotes: 2

Views: 988

Answers (1)

user539810
user539810

Reputation:

You need to ensure the port is available, else server& will be an invalid server handle. Choosing a port of 49152 or higher is generally safe. This probably isn't your only problem, however.

Your problem is likely that your connection variable is simply not the same, meaning HOST& and handle& should just be handle&. It's important to remember that there is never a "host handle" and a "client handle"; the only handles are the "server handle" (created using _OPENHOST to essentially reserve a port for your connections) and the "connection handle" (created using _OPENCONNECTION by the server to connect to a client or _OPENCLIENT by the client to connect to a server). This will also reduce your logic to just do a PUT, followed by a GET loop. I use the name connection& instead of handle&, but you get the idea.

SCREEN 12
CLS
PRINT ""
PRINT ""
PRINT ""
PRINT ""
PRINT ""
PRINT "                       POKELITE - By Mark "
PRINT ""
PRINT ""
INPUT "Join or Host a game? ", hostorjoin$
hostorjoin$ = UCASE$(hostorjoin$)
IF hostorjoin$ = "JOIN" THEN GOTO JOIN
IF hostorjoin$ = "HOST" THEN GOTO HOST
' If neither "HOST" nor "JOIN" is specified, what happens?

HOST:
server& = _OPENHOST("TCP/IP:300")
PRINT "Waiting for connection..."
PRINT "! Remember: If playing locally, give the other player your IPv4 Address !"
DO
    connection& = _OPENCONNECTION(server&)
LOOP UNTIL connection& <> 0
PRINT ""
PRINT "2nd Player Joined!"
SLEEP 2
GOTO GAME
JOIN:
INPUT "Enter Server IPv4 Address (Example: 192.168.1.25): ", joinip$
connection& = _OPENCLIENT("TCP/IP:300:" + joinip$)
IF connection& = 0 THEN PRINT "Connection failed!": SLEEP 2: CLS: GOTO JOIN
GOTO GAME
GAME:
CLS
INPUT "Enter your name: ", playerName$
IF playerName$ = "" THEN GOTO GAME
PRINT "Waiting for other player..."

' Send name to opponent and wait for opponent's name.
PUT connection&, , playerName$
DO
    GET connection&, , opponentName$
LOOP UNTIL opponentName$ <> ""

PRINT "You:     "; playerName$
PRINT "Opponent:"; opponentName$

Upvotes: 2

Related Questions