连接管理客户端只接收一次

时间:2015-12-01 19:11:59

标签: python sockets python-3.x

当我连接一些客户端后运行管理客户端时,我的管理员返回的IP地址和端口号很好。如果我关闭管理员并重新运行它没有任何反应。这令我感到困惑。我不确定为什么要这样做

#Admin Client
from functools import partial
import ssl
import socket

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

ts = ssl.wrap_socket(s, certfile="100298750.crt",
                        keyfile="100298750.key",
                        ca_certs="5cc515-root-ca.cer")

ts.connect(('192.168.0.5', 4001))

ts.send("Hello\r\n".encode())

if ts.recv(80).decode() == "Admin-Greetings\r\n":

    print("The players currently online are:\n")
    ts.send("Who\r\n".encode())

for data in iter(partial(ts.recv, 1000), b''):
    print(data.decode())
ts.close()

服务器

import threading
import socket
import math
import random
import ssl

addressList = []

def within(guess,goal,n):
    absValue = abs(guess - goal)
    if absValue <= n:
        return True
    else:
        return False

def HandleAdmin(adminSocket,):
    while True:
        global addressList

        (c,a) = adminSocket.accept() 

        ts = ssl.wrap_socket(c, certfile="5cc515_server.crt",
                                 keyfile="5cc515_server.key",
                                 server_side=True,
                                 cert_reqs=ssl.CERT_REQUIRED,
                                 ca_certs="5cc515-root-ca.cer")

        if ts.recv(80).decode() == 'Hello\r\n':

            ts.send('Admin-Greetings\r\n'.encode()) 

        if ts.recv(80).decode() == 'Who\r\n':
             for i in addressList:
                ts.send(i.encode())
        ts.close()
        return

def HandleClient(c,a):
    global addressList
    address, port = a
    address = str(address) + ' ' + str(port) + '\r\n'
    addressList.append(address)

    scoreCount = 0
    guess = 0
    if(c.recv(80).decode()) == 'Hello\r\n':
        c.send('Greetings\r\n'.encode())

        goal = random.randrange(1,21)

        while guess!= goal:
            guess =c.recv(80).decode()
            guess = int(guess[7:len(guess)-2])

            if guess == goal:
                c.send('Correct\r\n'.encode())
                addressList.remove(address)
                c.close()
            elif within(guess, goal, 2) == True:
                c.send('Close\r\n'.encode())
            else:
                c.send('Far\r\n'.encode())
    else:
        c.close()

    return

clientSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
clientSocket.bind(("192.168.0.5",4000))
clientSocket.listen(5)

adminSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
adminSocket.bind(("192.168.0.5",4001))
adminSocket.listen(5)

handleAdminThread = threading.Thread(target = HandleAdmin,
                                            args = (adminSocket,))
handleAdminThread.start()

while True:
    (c,a) = clientSocket.accept()
    clientThread = threading.Thread(target = HandleClient, args = (c,a))
    clientThread.start()

1 个答案:

答案 0 :(得分:0)

  

如果我关闭管理员并重新运行它没有任何反应。这有我   百思不得其解。我不确定为什么要这样做

它只是这样做,因为HandleAdmin()服务器线程代码具有行

        return

在循环结束时因此在一次运行后退出。放下它,没关系。