我无法让Ngrok使用Python套接字

时间:2019-01-29 00:21:30

标签: python multiplayer ngrok

因此,基本上,我用Python创建了一个带有套接字的小程序,该程序允许两个客户端通过服务器连接以彼此聊天。该程序运行完美,因此,我和我的朋友决定在不同的wi-fi上测试该程序。他在离我大约三十分钟的家中。我加载了Ngrok,并在“ ngrok.exe tcp 1234”中输入了1234。它给了我正常的文字,我给了我自己和我的朋友《转发》中的内容。我们放入IP和端口,annnnnd,它不起作用。客户端说已连接,但服务器说没有人连接。客户之间彼此不合作。请帮忙。

#SERVER#
#Import
from socket import *

#Variables
clientCount = 2

#Lists
clients = [[] for i in range(clientCount)]

#0 Username
clientData = [[] for i in range(clientCount)]

#Setup Socket
s = socket(AF_INET,SOCK_STREAM)

#Get IP And Port
IP = gethostbyname(gethostname())
port = 1234

#Set Up Server
s.bind((IP,port))

#Try To Locate Clients
s.listen(clientCount)

#Activates For Loop To Get Client Data
for i in range(clientCount):
    #Test If A Client Has Closed
    if i >= 1:
        if not clients[i-1][0].recv(1024):
            exit()

    #Accepts Clients If It Gets Any
    connection,addr = s.accept()

    clients[i].append(connection)
    clients[i].append(addr)

    #Receives Clients Usernames
    clientData[i].append(connection.recv(1024).decode("utf-8"))

    #Print Clients Data
    print("Received connection from '"+addr[0]+"' AKA '"+clientData[i][0]+"'")

#Sends Clients Usernames
for i in range(clientCount):
    if i == 0:
        clients[i][0].send(clientData[clientCount-1][0].encode("utf-8"))
        print("Sent '"+clients[i][1][0]+"'s username and ID to '"+clients[clientCount-1][1][0]+"'")
    else:
        clients[i][0].send(clientData[i-1][0].encode("utf-8"))
        print("Sent '"+clients[i][1][0]+"'s username and ID to '"+clients[i-1][1][0]+"'")

    clients[i][0].send(str(i+1).encode("utf-8"))

#Game Loop
while True:
    for i in range(clientCount):
        #Test If A Client Has Closed
        if not clients[i][0].recv(1024):
            exit()

        #Receive Messages
        message = clients[i][0].recv(1024).decode("utf-8")

        #Send Messages
        if i == 0:
            clients[clientCount-1][0].send(message.encode("utf-8"))
        else:
            clients[i-1][0].send(message.encode("utf-8"))


#CLIENT#
#Import
from socket import *

#Setup Socket
s = socket(AF_INET,SOCK_STREAM)

#Get Our IP, Server IP, And Port
ourIP = gethostbyname(gethostname())
print("Your IP address is: "+ourIP)

IP = input("Enter servers IP address: ")
port = int(input("Enter port: "))

#Ask User To Put In Username
name = input("Enter Username: ")

#Tells User That The Program Is Trying To Connect To The Server
print("Trying to connect to the server: "+IP+":"+str(port))

#Tries To Connect To Server
try:
    s.connect((IP,port))
except:
    print("Could not connect to server.")
    print("Press Enter to close program.")

    input()
    exit()

print("Connected!")

#Send Username To Server
s.send(name.encode("utf-8"))

#Receive Other Clients Username
otherName = s.recv(1024).decode("utf-8")

#Receive Which Client This Program Is
clientID = s.recv(1024).decode("utf-8")

#Game Loop
while True:
    #Test If The Server Has Closed
    if not s.recv(1024):
        exit()

    #Test If Client Has A Odd Number ID
    if int(clientID) % 2:
        #Send Messages
        s.send(input("> ").encode("utf-8"))

        #Receive Messages
        message = s.recv(1024).decode("utf-8")
        print(otherName+": "+message)

    else:
        #Receive Messages
        message = s.recv(1024).decode("utf-8")
        print(otherName+": "+message)

        #Send Messages
        s.send(input("> ").encode("utf-8"))

0 个答案:

没有答案