-我的主要问题是对套接字使用条件Python3同时发送和接收。 -so当我在一台设备上安装服务器和客户端,并且使用相同端口但另一台外部设备上使用不同IP地址的客户端和服务器,并且我想同时运行命令套接字时...使用按钮tkinter调用接受和拒绝来接受连接或拒绝
在两个设备的Python上都停止运行并阻止运行相同代码时
在客户端
def accept():
if accpt == False:
cnfrm = messagebox.askokcancel("accept","Would you like to ACCEPT ?")
if cnfrm == True:
print("connection is made........")
s.send(msg)
else:
print("connection denied...............")
s.close()
reply = s.recv(1024)
if reply:
print(str(reply))
else:
s.send("warning....no connection".encode('utf-8'))
pymsgbox.alert('exception. click OK to close session:', 'cancel')
s.close()
服务器端
import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_name = socket.gethostname() #To get the name of host
port_number = 8888
print("The name of local machine"),host_name
host_port_pair = (host_name,port_number) #A tuple
print(host_port_pair)
sock.bind(host_port_pair) #Bind address to the socket
sock.listen(10)
conn_obj,addr = sock.accept()
while True:
msg_from_client = conn_obj.recv(2048)
if not msg_from_client:
print("<...No Reply.from server.> ")
conn_obj.close()
else:
print("FROM CLIENT ===> ")
print(msg_from_client)
receive = msg_from_client
sock.close()
conn_obj.send("Thanks for your connection")
conn_obj.send(host_name.encode('utf-8'))
conn_obj.close()
#Closing the socket
sock.close()
已经尝试过带有循环的套接字recv,但是没有任何进展可以向我展示我在做什么错,关于deny()函数用于关闭套接字的情况,非常感谢