服务器不适用于多个客户端

时间:2016-04-21 07:15:00

标签: python multithreading sockets

请告诉我服务器代码有什么问题。它不能同时为两个客户工作。它仅针对我首先运行的客户端运行。我是python和socket编程的新手。请帮助我。

import socket
import sys
import thread
import time
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the address given on the command line
server_address = ('127.0.0.1', 10001)
data = " ".join(sys.argv[1:])
sock.bind(server_address)
print >>sys.stderr, 'starting up on %s port %s' % sock.getsockname()
sock.listen(1)
connection, client_address = sock.accept()
def my(threadName , delay):

    while True:
        print >>sys.stderr, 'waiting for a connection'

        try:
            print >>sys.stderr, 'client connected:', client_address
            while True:
                data = connection.recv(16)
                print >>sys.stderr, 'received "%s"' % data
                a=['a' ,'e', 'i' , 'o' , 'u']
                for i in data:
                    if i in a:
                        data = data.replace(i, '')

                        data=data.upper()
                        print data


                print'\nUpper case string:'

                if data:
                    connection.sendall(data)
                else:
                    break
        finally:
            connection.close()


thread.start_new_thread(my , ("Thread-1" , 8,))
thread.start_new_thread(my , ("Thread-2" , 10,))

2 个答案:

答案 0 :(得分:0)

查看 socketServer 模块,它可以轻松用于多个客户端 https://docs.python.org/3.4/library/socketserver.html

答案 1 :(得分:0)

主要问题是调用

connection, client_address = sock.accept()

只有一次,因此只接受一个连接,然后继续运行使用相同全局连接的2个线程。

相反,服务器应该接受循环中的新连接,并在接受连接时启动新的处理程序:

from itertools import count
from threading import Thread

if __name__ == '__main__':
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Bind the socket to the address given on the command line
    server_address = ('127.0.0.1', 10001)
    data = " ".join(sys.argv[1:])
    sock.bind(server_address)
    print >>sys.stderr, 'starting up on %s port %s' % sock.getsockname()
    sock.listen(1)

    for i in count(1):
        print >>sys.stderr, 'waiting for a connection'
        # Blocked here until a new connection is accepted
        connection, client_address = sock.accept()
        name = "Thread-%d" % i
        t = Thread(target=my,
                   args=(name, 8, connection, client_address),
                   name=name)
        t.start()

我冒昧地使用higher-level threading interface更改您的低级线程API使用情况。

处理程序函数应该接收连接作为参数而不是使用全局变量,因为连接对于该处理程序是唯一的。

from contextlib import closing

def my(threadName, delay, connection, client_address):
    print >>sys.stderr, 'client connected:', client_address
    with closing(connection):
        while True:
            data = connection.recv(16)
            print >>sys.stderr, 'received %r' % data
            a = {'a' ,'e', 'i' , 'o' , 'u'}
            for i in data:
                if i in a:
                    data = data.replace(i, '')

            data = data.upper()    
            print 'Upper case string:', data

            if data:
                connection.sendall(data)

            else:
                break