title: twisted: test if connection exists before writing to transport
body: <p>Is there a possibility to test if the connection still exists before executing a <code>transport.write()</code>?</p>
  
  <p>I have modified the simpleserv/simpleclient examples so that a message is being send (written to <code>Protocol.transport</code>) every 5 seconds. The connection is persistent.</p>
  
  <p>When disconnecting my wifi, it still writes to transport (of course the messages don't arrive on the other side) but no error is thrown.
  When enabling the wifi again, the messages are being delivered, but <em>the next attempt</em> to send a message fails (and <code>Protocol.connectionLost</code> is called).</p>
  
  <p>Here again what happens chronologically:</p>
  
  <ol>
  <li>Sending a message establishes the connection, the message is delivered.</li>
  <li>Disabling wifi</li>
  <li>Sending a message writes to <code>transport</code>, does not throw an error, the message does not arrive</li>
  <li>Enabling wifi</li>
  <li>Message sent in 3. arrives</li>
  <li>Sending a message results in <code>Protocol.connectionLost</code> call</li>
  </ol>
  
  <p>It would be nice to know before executing step 6 if I can write to transport. Is there any way?</p>
  
  <p><strong>Server</strong>:</p>
  
  <pre><code># Copyright (c) Twisted Matrix Laboratories.
  # See LICENSE for details.
  
  
  from twisted.internet import reactor, protocol
  
  
  class Echo(protocol.Protocol):
      """This is just about the simplest possible protocol"""
  
      def dataReceived(self, data):
          "As soon as any data is received, write it back."
          print
          print data
          self.transport.write(data)
  
  def main():
      """This runs the protocol on port 8000"""
      factory = protocol.ServerFactory()
      factory.protocol = Echo
      reactor.listenTCP(8000,factory)
      reactor.run()
  
  # this only runs if the module was *not* imported
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p><strong>Client:</strong></p>
  
  <pre><code># Copyright (c) Twisted Matrix Laboratories.
  # See LICENSE for details.
  
  
  """
  An example client. Run simpleserv.py first before running this.
  """
  
  from twisted.internet import reactor, protocol
  
  # a client protocol
  
  counter = 0
  
  class EchoClient(protocol.Protocol):
      """Once connected, send a message, then print the result."""
  
      def connectionMade(self):
          print 'connectionMade'
  
      def dataReceived(self, data):
          "As soon as any data is received, write it back."
          print "Server said:", data
  
      def connectionLost(self, reason):
          print "connection lost"
  
      def say_hello(self):
          global counter
          counter += 1
          msg = '%s. hello, world' %counter
          print 'sending: %s' %msg
          self.transport.write(msg)
  
  class EchoFactory(protocol.ClientFactory):
  
      def buildProtocol(self, addr):
          self.p = EchoClient()
          return self.p
  
      def clientConnectionFailed(self, connector, reason):
          print "Connection failed - goodbye!"
  
      def clientConnectionLost(self, connector, reason):
          print "Connection lost - goodbye!"
  
      def say_hello(self):
          self.p.say_hello()
          reactor.callLater(5, self.say_hello)
  
  # this connects the protocol to a server running on port 8000
  def main():
      f = EchoFactory()
      reactor.connectTCP("REMOTE_SERVER_ADDR", 8000, f)
      reactor.callLater(5, f.say_hello)
      reactor.run()
  
  # this only runs if the module was *not* imported
  if __name__ == '__main__':
      main()
  </code></pre>