title: Send multiple messages to server from twisted client
body: <p>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.</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>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. </p>
  
  <pre><code>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("&gt;")
          if r == 'q' or len(r) == 0: break
          sendMessage(r)
  </code></pre>
  
  <p>What might be wrong? this is the error message. </p>
  
  <pre><code>&gt;a
  Server said: a
  Connection lost.
  &gt;b
  Traceback (most recent call last):
    File "echoclient.py", line 38, in &lt;module&gt;
      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
  </code></pre>