即使发送文本后,如何让插座客户端始终打开工作?
我有一个使用套接字将预先建立的“ HI”文本发送到服务器的代码。
当您完成发送文本“ HI”时,客户端关闭。
即使发送“ HI”
,我也希望客户保持开放我为什么要这个?因为我想在tkinter中创建一个按钮,因此每次按下它,就会出现“ hi”。但是我第一次按“嗨”,客户关闭,不可能按其他时间。
我应该怎么办?
服务器
from socket import *
import cv2
imagem = cv2.imread("foto.png")
host = gethostname()
port = 7777
print(f'HOST: {host} , PORT {port}')
serv = socket(AF_INET, SOCK_STREAM)
serv.bind((host, port))
serv.listen(5)
con, adr = serv.accept()
msg = con.recv(1024).decode()
print(msg)
if msg == "hi":
cv2.imshow("Original", imagem)
cv2.waitKey(0)
客户端
from socket import *
from tkinter import *
root = Tk()
root.geometry('430x300')
host = gethostname()
port = 7777
cli = socket(AF_INET, SOCK_STREAM)
cli.connect((host, port))
def bt4():
msg = ("hi")
cli.send(msg.encode())
btn = Button(root, text='hi', width=40, height=5, bd='10', command=bt4)
btn.place(x=65, y=100)
root.mainloop()
与按钮的TKINTER接口,只能按一次发送“ HI”:
< img src =“ https://i.sstatic.net/vsvtc.png” alt =“在此处输入图像说明”>
I have a code that sends a pre-established "hi" text to a server using socket.
When you finish sending the text "hi", the client closes.
I would like the client to remain open, even after sending the "hi"
Why do I want this? Because I want to create a button in tkinter, so that every time I press it, the "hi" appears. But the first time I press "hi", the client closes, and it is not possible to press other times.
What should I do?
Server
from socket import *
import cv2
imagem = cv2.imread("foto.png")
host = gethostname()
port = 7777
print(f'HOST: {host} , PORT {port}')
serv = socket(AF_INET, SOCK_STREAM)
serv.bind((host, port))
serv.listen(5)
con, adr = serv.accept()
msg = con.recv(1024).decode()
print(msg)
if msg == "hi":
cv2.imshow("Original", imagem)
cv2.waitKey(0)
Client
from socket import *
from tkinter import *
root = Tk()
root.geometry('430x300')
host = gethostname()
port = 7777
cli = socket(AF_INET, SOCK_STREAM)
cli.connect((host, port))
def bt4():
msg = ("hi")
cli.send(msg.encode())
btn = Button(root, text='hi', width=40, height=5, bd='10', command=bt4)
btn.place(x=65, y=100)
root.mainloop()
The tkinter interface with the button, it is possible to press only once to send a "hi":
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道自己做了什么,但我理解,我设法解决了它,起初它起作用了。
插座
客户端
I don't know exactly what I did, but I understood, and I managed to solve it, at first it worked.
Socket
Client