在多个端口Echo Server python上接收多条消息

时间:2013-11-29 03:32:35

标签: python tcp echo

我正在用Python编写一个echo服务器和客户端,它实现了一个简单的猜数游戏。我知道如何使用select进行多路复用,这很好。我写的另一台服务器实现了这一点。但是现在我正在编写一个新服务器(它非常相似),但它接受来自两个端口的连接,而不是一个端口,一个端口用于播放器客户端,一个用于管理员,我将最终用于who命令,返回所有连接的播放器。 我的问题是,在发送初始问候消息后,客户端会在第一次发送时收到服务器的反馈recv。但之后我无法向服务器发送任何消息(没有任何内容从客户端发送),我一直在搜索和玩几个小时,但无济于事。任何帮助,将不胜感激。谢谢!

# MULTIPLEX SERVER

import socket, select, time, random, ssl, sys, os

# VARS

EXP = 1
HOST = '127.0.0.1'
PORT_P = 4000
PORT_A = 4001
BUFFSZ = 1024
BKLOG = 5
GREETS = 'Greetings'
INPUTS = []
OUTPUTS = []
CLIENT_ADDRS = {}
CLIENT_ANS = {}
CLIENTS = ""
_adm_rtnMSG = 'Admin_Greetings'

# Function to determine how far the player is
# from the chosen random number

def Within(value, target):
    diff = abs(target - value)
    if diff > 3:
        return 'Not even close, youth!'
    else:
        return 'Ooh, not to far: ' + str(diff) + ' away, keep trying...'

# END_FUNCTION

print('Server up and running...\n')

try:
    for p in PORT_P, PORT_A:
        INPUTS.append(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
        INPUTS[-1].setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        INPUTS[-1].bind((HOST, p))
        INPUTS[-1].listen(BKLOG)
except socket.error(value, message): 
    if INPUTS[-1]:
        INPUTS[-1].close()
        INPUTS = INPUTS[:-1]
    print('Failure to open socket: ' + message)
    sys.exit(1)

while True:
    READ_IO, WRITE_IO, ERROR = select.select(INPUTS, OUTPUTS, INPUTS)
    for r in READ_IO:
        for p in INPUTS:
            if r is p:
                (acpt_sock, addr) = p.accept()
                print('Connection established with ', acpt_sock.getsockname())
                CLIENT_ADDRS[acpt_sock] = addr
                CLIENT_ANS[acpt_sock] = random.randrange(1, 20)
            else:
                data = acpt_sock.recv(BUFFSZ).decode()
                acpt_sock.setblocking(0)
                if data:
                    if 'Hello' in data:
                        print(CLIENT_ADDRS[acpt_sock], ' random number is: ', CLIENT_ANS[acpt_sock])
                    acpt_sock.send(b'Greetings\nGuess a random number between 1 & 20')
                # drop elif here for admin cmd
                elif 'Hi' in data:
                    acpt_sock.send(_adm_rtnMSG.encode())
                else:
                   if int(data) == CLIENT_ANS[acpt_sock]:
                        acpt_sock.send(b'That was correct, Well done!')
                   else:
                        acpt_sock.send(str(Within(int(data), CLIENT_ANS[acpt_sock])).encode())
            else:
                print('Closing Connection @ ', addr)
                INPUTS.remove(acpt_sock)
                acpt_sock.close()
                del CLIENT_ADDRS[acpt_sock]


# PLAYER CLIENT

import socket
import re

# INIT VARS
HOST = '127.0.0.1'
PORT = 4000
INITSTR = 'Hello'
BUFF = 1024

# Set up socket
sender = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sender.connect((HOST, PORT))
sender.send(bytes((INITSTR), "ascii"))

print("Kirby Prompt FTW!\nConnected to Server via", HOST, "::", PORT, '\n')

rtnMSG = sender.recv(BUFF).decode()
print(rtnMSG)

# Simple loop to keep client alive
# to send and receive data from the server
while 'correct' not in rtnMSG:
    _guess = input("(>',')> ")
    sender.send(bytes((_guess), "ascii"))
    rtnMSG = sender.recv(BUFF).decode()
    print(rtnMSG)
sender.close()

# ADMIN CLIENT

import socket
import re
import ssl

# INIT VARS
HOST = '127.0.0.1'
PORT = 4001
INITSTR = 'Hi'
BUFF = 1024

# Set up socket
adm_sender = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
adm_sender.connect((HOST, PORT))
adm_sender.send(bytes((INITSTR), "ascii"))

print("Connected to Server as Admin via", HOST, "::", PORT, '\n')

rtnMSG = adm_sender.recv(BUFF).decode()
print(rtnMSG)

while True:
    cmd = input('$ ')
    adm_sender.send(bytes((cmd), "ascii"))
    rtnMSG = adm_sender.recv(BUFF).decode()
    print(rtnMSG)
adm_sender.close()

0 个答案:

没有答案