创建套接字时未定义套接字
Socket is not defined when I have created one
我有一个程序可以通过 tkinter 接口向服务器发送消息。我正在使用各种子程序来 运行 一些基本的事情,比如开始一个新的连接和发送一条消息。唯一的问题是,当用户 必须 通过另一个子程序建立连接时,它会不断出现名称错误 "clientsocket is not defined"。
我试过移动子程序,使 clientsocket 成为全局变量,并且我仔细检查了我的名字是否正确。我从函数中获得这些子程序的代码(我刚才在控制台中 运行 的一个聊天程序)所以它一定是我构建程序的方式。
这里是所有处理套接字的子程序
def send_msg(): # Sends a message to the server (called from tkinter button)
try:
Csend = field.get("1.0", "end-1c")
Csend = str(Csend)
clientsocket.send(Csend.encode())
field.delete()
field.insert(END, "")
except error as err:
Csend = field.get("1.0", "end-1c") # 1.0 - Line one, char zero
print(f"Can't send message \"{Csend}\": Not connected to a server")
print(err)
def new_connection():
# Tkinter code goes here
def connect_server(): # Tries to connect to the server
try: # This runs the first check, if there is an existing connection
clientsocket.close()
except:
print("No existing connection")
try: # Runs the second check, if it can connect using the info
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = host_ent.get() # Gets STR of host IP
port = port_ent.get() # Gets STR of port
port = int(port) # Converts port to INT
clientsocket.connect((host, port)) # Connects to the server
except:
print("Could not connect, incorrect inputs?")
def disconnect_server():
try:
clientsocket.close()
except:
print("No existing connection")
所有这些子程序都是从 tkinter 菜单栏或按钮调用的。
我对套接字很陌生,所以如果有任何代码是初学者级别的,那么我很抱歉。
提前感谢您的帮助!
问题是,你没有定义你的套接字全局,只在你的函数中定义局部。您应该在使用 clientsocket 的每个函数的 header 下添加 global clientsocket
。
我有一个程序可以通过 tkinter 接口向服务器发送消息。我正在使用各种子程序来 运行 一些基本的事情,比如开始一个新的连接和发送一条消息。唯一的问题是,当用户 必须 通过另一个子程序建立连接时,它会不断出现名称错误 "clientsocket is not defined"。
我试过移动子程序,使 clientsocket 成为全局变量,并且我仔细检查了我的名字是否正确。我从函数中获得这些子程序的代码(我刚才在控制台中 运行 的一个聊天程序)所以它一定是我构建程序的方式。
这里是所有处理套接字的子程序
def send_msg(): # Sends a message to the server (called from tkinter button)
try:
Csend = field.get("1.0", "end-1c")
Csend = str(Csend)
clientsocket.send(Csend.encode())
field.delete()
field.insert(END, "")
except error as err:
Csend = field.get("1.0", "end-1c") # 1.0 - Line one, char zero
print(f"Can't send message \"{Csend}\": Not connected to a server")
print(err)
def new_connection():
# Tkinter code goes here
def connect_server(): # Tries to connect to the server
try: # This runs the first check, if there is an existing connection
clientsocket.close()
except:
print("No existing connection")
try: # Runs the second check, if it can connect using the info
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = host_ent.get() # Gets STR of host IP
port = port_ent.get() # Gets STR of port
port = int(port) # Converts port to INT
clientsocket.connect((host, port)) # Connects to the server
except:
print("Could not connect, incorrect inputs?")
def disconnect_server():
try:
clientsocket.close()
except:
print("No existing connection")
所有这些子程序都是从 tkinter 菜单栏或按钮调用的。
我对套接字很陌生,所以如果有任何代码是初学者级别的,那么我很抱歉。
提前感谢您的帮助!
问题是,你没有定义你的套接字全局,只在你的函数中定义局部。您应该在使用 clientsocket 的每个函数的 header 下添加 global clientsocket
。