我遇到了如何从N客户端向代理发送号码的问题。从客户端到服务器的代理转发号码。服务器计算平均值并发送回代理,最后客户端从代理获得回复。
我在 TCP套接字(使用Python)下面使用。我想按照以下方式做:: 如果有4个客户端发送整数,如10,10,10,10到代理,然后是END,则代理接收它们为{10,10,10,10,end}并发送到服务器。并且在计算平均值(例如,avg = 10 + 10 + 10 + 10/4 = 10)到代理之后回复服务器,然后代理将平均结果发送回客户端。
我在客户端/代理/服务器之间创建了连接,但是我遇到使用SELECT发送整数的问题。
请检查以下代码,并建议如何使用SELECT(在Python TCP套接字中)实现此功能。
对于客户::::
import socket
HOST = 'localhost'
PORT = 21001
proxysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
proxysock.connect((HOST, PORT))
while 1:
x = int(raw_input())
proxysock.sendall(x)
data = proxysock.recv(1024)
proxysock.close()
For Proxy ::
import socket
from thread import *
HOST = 'localhost'
PORT = 21001
max_conn = 5
buffer_size = 2048
def start():
proxyserversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
proxyserversock.bind((HOST, PORT))
proxyserversock.listen(max_conn)
while 1:
client_sock, addr = proxyserversock.accept()
data = client_sock.recv(buffer_size)
serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversock.connect((HOST, PORT))
serversock.sendall(X)
serversock.recv(buffer_size)
client_sock.sendall()
serversock.close()
client_sock.close()
对于Server ::
import socket
HOST = 'localhost'
PORT = 21001
serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversock.bind((HOST, PORT))
s.listen(10)
while 1:
proxyclientsock, addr = serversock.accept()
data = proxyclientsock.recv(2048)
proxyclientsock.sendall()
proxyclientsock.close()