我正在开发一种客户端 - 服务器架构,其中多个客户端应将文件传输到正在运行的服务器。我想知道服务器是否可以同时从不同的客户端接收一个端口上的输入。
到目前为止我的代码:
服务器
import socket
import time
mySocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
# EDIT: This line was added based on @Aleksander Gurin's response below. The problem persists.
mySocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mySocket.bind( ('localhost', 1234 ) )
mySocket.listen( 2 )
channel, details = mySocket.accept()
while True:
incoming = channel.recv( 100 )
if incoming:
print "Received >%s<" % incoming
incoming = ''
客户端
import sys
import socket
import time
mySocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
mySocket.connect( ('127.0.0.1', 1234) )
counter = 1
while True:
mySocket.send( "Message %03d from client %s." % (counter, sys.argv[1]) )
time.sleep(2)
counter += 1
我正在启动客户端的两个实例:
./client.py 1 &
./client.py 2 &
然而,到目前为止,我的服务器只接收来自一个客户端的输入:
Received >Message 001 from client 1.<
Received >Message 002 from client 1.<
Received >Message 003 from client 1.<
Received >Message 004 from client 1.<
Received >Message 005 from client 1.<
...
我的问题是:是否有可能从服务器上的第二个客户端收到帖子 - 如果是的话,怎么样?
P上。 S。:我检查了this related SO post,但无法真正从中提取答案。
答案 0 :(得分:1)
您需要在SO_REUSEADDR
套接字之前设置bind
套接字选项。这就是所谓的socket-level option。结果服务器将锁定如下内容:
import socket, threading
def handler(s):
data = "none"
while len(data):
data = s.recv(4096)
print "Data: %s"%(data)
s.close()
if __name__ == "__main__":
tcpsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# here you set socket options, this is what you need
tcpsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# than you bind the socket
tcpsocket.bind(('', 1234))
tcpsocket.listen(5)
while True:
s, (ip, port) = tcpsocket.accept()
threading.Thread(target = handler, args = (s,)).start()