在 QB64 中通过网络接收和发送字符串
Receiving and Sending Strings over the Network in QB64
我正在为我的兄弟开发一款可在网络上运行的迷你口袋妖怪游戏。
不幸的是,在测试时我发现由于某种原因,它仅在尝试将字符串发送到另一台计算机的行上给出了关于 "Bad file name or number" 的错误,但在循环接收命令时没有错误。
这是我的代码:
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$
您需要确保端口可用,否则server&
将是无效的服务器句柄。 Choosing a port of 49152 or higher is generally safe。然而,这可能不是您唯一的问题。
您的问题很可能是您的连接变量根本不一样,这意味着 HOST&
和 handle&
应该只是 handle&
。重要的是要记住,从来没有 "host handle" 和 "client handle";唯一的句柄是 "server handle"(使用 _OPENHOST
创建,实质上为您的连接保留一个端口)和 "connection handle"(服务器使用 _OPENCONNECTION
创建以连接到客户端或 _OPENCLIENT
由客户端连接到服务器)。这也将减少您的逻辑,只执行 PUT
,然后执行 GET
循环。我使用名称 connection&
而不是 handle&
,但你明白了。
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$
我正在为我的兄弟开发一款可在网络上运行的迷你口袋妖怪游戏。 不幸的是,在测试时我发现由于某种原因,它仅在尝试将字符串发送到另一台计算机的行上给出了关于 "Bad file name or number" 的错误,但在循环接收命令时没有错误。
这是我的代码:
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$
您需要确保端口可用,否则server&
将是无效的服务器句柄。 Choosing a port of 49152 or higher is generally safe。然而,这可能不是您唯一的问题。
您的问题很可能是您的连接变量根本不一样,这意味着 HOST&
和 handle&
应该只是 handle&
。重要的是要记住,从来没有 "host handle" 和 "client handle";唯一的句柄是 "server handle"(使用 _OPENHOST
创建,实质上为您的连接保留一个端口)和 "connection handle"(服务器使用 _OPENCONNECTION
创建以连接到客户端或 _OPENCLIENT
由客户端连接到服务器)。这也将减少您的逻辑,只执行 PUT
,然后执行 GET
循环。我使用名称 connection&
而不是 handle&
,但你明白了。
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$