无法在 Socket Server 中获取其他消息

问题描述 投票:0回答:1

当我尝试向服务器发送消息时,1 条消息后没有问题,但当我尝试发送另一条消息时,它失败了 因为我的应用程序有问题,我不希望没有人想要这样。

客户:

import socket as soc

# Defining The Variables
HOST = '192.168.0.21'  # DON'T Forget To Change it For your computer's Ip Address.
PORT = 1024
client_socket = soc.socket()
Shutdown_Request = "Request_From_Phone_to_Shutdown"
Restart_Request = "Request_From_Phone_to_Restart"

client_socket.connect((HOST, PORT))


def shutdown_btn(e=None):  # Sending Shutdown Request
    client_socket.sendall(Shutdown_Request.encode('utf-8'))
    client_socket.close()


def restart_btn(e=None):  # Sending Restart Request
    client_socket.sendall(Restart_Request.encode('utf-8'))
    client_socket.close()

服务器:

import socket as soc
import os

os.system("cls"); os.system("color e"); os.system("title  Server: WakeMate")
print("   Server is Running... ")

def send_ip_to_client():
    pass
print(" Warning: u may forget the line of 11")

def server_program():
    
    #  getting the hostname.
    host = soc.gethostname()
    port = 1024  # initiate port no above 1024.

    server_socket = soc.socket()  # get instance
    server_socket.bind((host, port))  # bind host address and port together
    server_socket.listen() # configure how many clients the server can listen to simultaneously

    while True:
        conn, address = server_socket.accept()  # accept new connection
        print(f"Connection from: {address}")

        while True:
            data_get = conn.recv(1024)
            if not data_get:
                break
            data_convert = data_get.decode('utf-8')
            # Print The Message.
            print(f"Message from client: {data_convert}")
            if data_convert == 'Request_From_Phone_to_Shutdown':
                print("Line 33 is missing.")
                #os.system("shutdown /s /f /t 0")
            elif data_convert == 'Request_From_Phone_to_Restart':
                print("Line 36 is missing.")
                #os.system("shutdown /r /t 0")

        #conn.close()  # close the connection

if __name__ == '__main__':
    server_program()

我想不间断地发送多条消息

python python-3.x sockets
1个回答
0
投票

无论如何,伙计们,我刚刚删除了“

client_socket.close()
”行,它就得到了修复。

© www.soinside.com 2019 - 2024. All rights reserved.