2017-07-17 116 views
2

我正在爲我的兄弟工作在網絡上的迷你口袋妖怪遊戲。 不幸的是,在測試時,我發現出於某種原因,它只在嘗試向另一臺計算機發送字符串的行上出現「錯誤的文件名或數字」錯誤,但在循環接收命令時沒有錯誤。在QB64中通過網絡接收和發送字符串

這裏是我的代碼:

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$ 

回答

2

您需要確保端口可用,否則server&將是無效的服務器手柄。 Choosing a port of 49152 or higher is generally safe。然而,這可能不是你唯一的問題。

您的問題很可能是您的連接變量根本不相同,這意味着HOST&handle&應該只是handle&。重要的是要記住,從來沒有「主機句柄」和「客戶端句柄」;唯一的句柄是「服務器句柄」(使用_OPENHOST爲本地連接創建一個端口)和「連接句柄」(由服務器使用_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$ 
相關問題