Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的并行服务器_Python_Multithreading_Sockets_Tcp - Fatal编程技术网

Python中的并行服务器

Python中的并行服务器,python,multithreading,sockets,tcp,Python,Multithreading,Sockets,Tcp,我想实现一个并行服务器。服务器应同时接受多个客户端。以便客户端可以向服务器发送消息 串行服务器工作得很好。我可以连接、写入、关闭并可以再次连接,没有问题。现在我想实现线程。就像:对于每个新客户机,都必须有一个新线程来处理一个带有一个客户机的TCP套接字 我的串行服务器代码: #!/usr/bin/python # This is server.py file import socket # Import socket module imp

我想实现一个并行服务器。服务器应同时接受多个客户端。以便客户端可以向服务器发送消息

串行服务器工作得很好。我可以连接、写入、关闭并可以再次连接,没有问题。现在我想实现线程。就像:对于每个新客户机,都必须有一个新线程来处理一个带有一个客户机的TCP套接字

我的串行服务器代码:

    #!/usr/bin/python           # This is server.py file

import socket               # Import socket module
import time

while True:
   s = socket.socket()         # Create a socket object
   host = socket.gethostname() # Get local machine name
   port = 12345                # Reserve a port for your service.
   s.bind((host, port))        # Bind to the port

   s.listen(5)                 # Now wait for client connection.

   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   c.send('Please wait...')
   time.sleep(2)
   c.send('Thank you for connecting with your admin. Please write now.')

   while True:
      msg = c.recv(1024)
      if not msg:
         s.close()
         break
      elif msg == "close1234567890":
         print ("Connection with %s was closed by the client." % (addr[0]))
      else:
         print "%s: %s" % (addr[0], msg)
我对并行服务器的尝试:

import socket              
import time
import thread


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
s.bind((host, 50999))       
s.listen(5)                

def session(conn, addr):
   while True:
      print 'Got connection from', addr
      conn.send('Please wait...')
      time.sleep(2)
      conn.send('Thank you for connecting with your admin. Please write now.')

      while True:
         msg = conn.recv(1024)
         if not msg:
            s.close()
            break
         elif msg == "close1234567890":
            print ("Connection with %s was closed by the client." % (addr[0]))
         else:
            print "%s: %s" % (addr[0], msg)

while True:
    conn, addr = s.accept()
    try:
       thread.start_new_thread(session(conn, addr))
    finally:
       s.close()
错误:我启动了服务器,没有问题。然后我启动客户端,一切正常。我会写,信息由服务器打印出来。然后我启动了第二个客户端,但是在这个窗口中什么都没有发生。没有机会从第二个客户端写入


Sry,我绝对是线程的初学者;)

这是因为您
s.close()
socket。以下是修改后的代码:

def session(conn, addr):
    while True:
        print 'Got connection from', addr
        conn.send('Please wait...')
        time.sleep(2)
        conn.send('Thank you for connecting with your admin. Please write now.')

        while True:
            msg = conn.recv(1024)
            if not msg:
                conn.close()
                break
            elif msg == "close1234567890":
                print ("Connection with %s was closed by the client." % (addr[0]))
            else:
                print "%s: %s" % (addr[0], msg)

while True:
    conn, addr = s.accept()
    thread.start_new_thread(session(conn, addr))

s.close()
我已经测试过了,效果很好。顺便说一句,我更改您的:

host = socket.gethostname()
s.bind((host, 50999))

s.bind(('localhost',50999))
。我不确定您为什么需要机器名,而原始代码根本不起作用——将主机名绑定到套接字没有意义

Sry,但它不适用于我的客户端代码。也许这是客户端的错误。我不认为,我必须更新这个代码,太。但是也许。。。