简体   繁体   English

从扭曲的客户端向服务器发送多条消息

[英]Send multiple messages to server from twisted client

I'm trying to build a client/server system that clients send messages to server. 我试图建立一个客户端/服务器系统,客户端将消息发送到服务器。 Server does nothing more than printing out what clients send. 服务器只做输出客户端发送的内容。

from twisted.internet import protocol, reactor

class Echo(protocol.Protocol):
    def dataReceived(self, data):
        print data
        self.transport.write(data)

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return Echo()

reactor.listenTCP(8000, EchoFactory())
reactor.run()

The issue is that when I tried to send multiple message from client with this code, the client raises an error after the first connection and send. 问题是,当我尝试使用此代码从客户端发送多条消息时,客户端在首次连接并发送后引发错误。

from twisted.internet import reactor, protocol
import time

class EchoClient(protocol.Protocol):
   def __init__(self, message):
       self.message = message

   def connectionMade(self):
       self.transport.write(self.message)

   def dataReceived(self, data):
       print "Server said:", data
       self.transport.loseConnection()

class EchoFactory(protocol.ClientFactory):
   def __init__(self, message):
       self.message = message

   def buildProtocol(self, addr):
       return EchoClient(self.message)

   def clientConnectionFailed(self, connector, reason):
       print "Connection failed."
       reactor.stop()

   def clientConnectionLost(self, connector, reason):
       print "Connection lost."
       reactor.stop()

def sendMessage(message):
    reactor.connectTCP("localhost", 8000, EchoFactory(message))
    reactor.run()

if __name__ == "__main__":
    while True:
        r = raw_input(">")
        if r == 'q' or len(r) == 0: break
        sendMessage(r)

What might be wrong? 可能是什么问题? this is the error message. 这是错误消息。

>a
Server said: a
Connection lost.
>b
Traceback (most recent call last):
  File "echoclient.py", line 38, in <module>
    sendMessage(r)
  File "echoclient.py", line 32, in sendMessage
    reactor.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/base.py", line 1168, in run
    self.startRunning(installSignalHandlers=installSignalHandlers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/base.py", line 1148, in startRunning
    ReactorBase.startRunning(self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/base.py", line 680, in startRunning
    raise error.ReactorNotRestartable()
twisted.internet.error.ReactorNotRestartable

Although this question is old and has two irrelevant answers, I would like to answer it for the people who are eager to know. 尽管这个问题比较老,有两个不相关的答案,但我想为渴望了解的人们回答。 The problem is that in your server code in the dataReceived method you have self.transport.write(data) (which sends every message that it receives to the client), and at the same time in your client code, again in the dataReceived method you have the command self.transport.loseConnection() (which looses the connection to the server as soon as a message comes in). 问题在于,在服务器代码中的dataReceived方法中,您具有self.transport.write(data) (它将接收到的每条消息发送给客户端),同时在客户端代码中,您也具有dataReceived方法您具有命令self.transport.loseConnection()self.transport.loseConnection() ,它将断开与服务器的连接)。 So, if you remove any of these lines you should be fine. 因此,如果删除这些行中的任何一条,都应该没问题。 In the current setting the first message sent from client will be sent back to the client and that will cause a connection disconnect. 在当前设置中,将从客户端发送的第一条消息发送回客户端,这将导致连接断开。

Also, you are calling reactor.run () at each attempt for sending a message. 另外,每次发送消息时,您都在调用Reactor.run()。 Reactor.run should be called only once preferably in the main function. 在主函数中最好只调用一次Reactor.run。

反应器不可重启。

For my own purposes, using socket to send messages to server works fine. 出于我自己的目的,使用套接字将消息发送到服务器可以正常工作。

import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 8000))
while True:
    a = raw_input("> ")
    if a == 'q' or len(a) == 0: 
        client_socket.close()
        break
    else:
        client_socket.send(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM