即使发送文本后,如何让插座客户端始终打开工作?

发布于 2025-01-31 07:52:20 字数 1214 浏览 2 评论 0原文

我有一个使用套接字将预先建立的“ HI”文本发送到服务器的代码。

  1. 当您完成发送文本“ HI”时,客户端关闭。

  2. 即使发送“ HI”

    ,我也希望客户保持开放
  3. 我为什么要这个?因为我想在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.

  1. When you finish sending the text "hi", the client closes.

  2. I would like the client to remain open, even after sending the "hi"

  3. 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":

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2025-02-07 07:52:20

我不知道自己做了什么,但我理解,我设法解决了它,起初它起作用了。

插座

from socket import *
import cv2
imagem = cv2.imread("foto.png")

host = gethostname()
port = 4444

print(f'HOST: {host} , PORT {port}')
serv = socket(AF_INET, SOCK_STREAM)
serv.bind((host, port))
serv.listen(5)


while 1:
    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')



def bt4():
    host = gethostname()
    port = 4444
    cli = socket(AF_INET, SOCK_STREAM)
    cli.connect((host, port))
    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()

I don't know exactly what I did, but I understood, and I managed to solve it, at first it worked.

Socket

from socket import *
import cv2
imagem = cv2.imread("foto.png")

host = gethostname()
port = 4444

print(f'HOST: {host} , PORT {port}')
serv = socket(AF_INET, SOCK_STREAM)
serv.bind((host, port))
serv.listen(5)


while 1:
    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')



def bt4():
    host = gethostname()
    port = 4444
    cli = socket(AF_INET, SOCK_STREAM)
    cli.connect((host, port))
    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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文