title: twisted python self.transport is not working just after connecting to server
body: <p>I have been working on a <a href="https://github.com/vaizguy/cryptikchaos" rel="nofollow">P2P chat application</a> based on kivy and twisted frameworks, I was hoping to get a lay of the land but I have run into this problem where if a client needs to connect to another client (via its server) it needs to perform a kind of handshake,</p>
  
  <p>now the initial step was to connect to the client ;</p>
  
  <pre><code>conn = reactor.connectTCP(host, port, CommCoreClientFactory(self))
  </code></pre>
  
  <p>and then write into the connection;</p>
  
  <pre><code>conn.transport.write("data..\r\n")
  </code></pre>
  
  <p><strong>Here the connection succeeds but the line doesn't go through</strong>, I have condensed the above code as per my intent, please see the method add_peer_to_swarm(self, pid, host) in comm/twisscomm.py</p>
  
  <p>my clientProtocol/Factory and serverProtocol/Factory code can be found below;
  (they can be found in comm/commcoreclient.py and comm/commcoreserver.py resp.)</p>
  
  <p>Client Protocol</p>
  
  <pre><code>class CommCoreClientProtocol(LineReceiver):
  
  """
  Communications core client protocol code.
  """
  
  def __init__(self, factory):
  
      self._peer_host = None
      self._peer_port = None
      self._peer_repr = None
      self.factory    = factory
  
  def connectionMade(self):
      "Run when connection is established with server."
  
      self._peer_host = self.transport.getPeer().host
      self._peer_port = self.transport.getPeer().port
      self._peer_repr = self._peer_host + " on " + str(self._peer_port)
  
      Logger.debug(
          "Connection success! Connected to {}".format(self._peer_repr))
  
  def connectionLost(self, reason):
      "Run when connection is lost with server."
  
      Logger.warn("Lost connection with peer {}".format(self._peer_repr))
  
  def lineReceived(self, line):
      "Run when response is recieved from server."
  
      response = self.factory.app.handle_response(line)
  
      if response:
          print response
  
      Logger.debug("Recieved : {}".format(base64.b64encode(line)))
  </code></pre>
  
  <p>Client Factory</p>
  
  <pre><code>class CommCoreClientFactory(protocol.ReconnectingClientFactory):
  
  protocol = CommCoreClientProtocol
  
  def __init__(self, app):
  
      self.app = app
  
  def startedConnecting(self, connector):
      "Run when initiaition of connection takes place."
  
      Logger.debug("Attempting connection...")
  
  def buildProtocol(self, addr):
      "Build protocol on successful connection."
  
      Logger.debug("Connected.")
      Logger.debug("Resetting reconnection delay.")
  
      # Reset the delay on connection success
      self.resetDelay()
  
      # Overridden build protocol
      #client_protocol = self.protocol()
      #client_protocol.factory = self
      #return client_protocol
  
      return CommCoreClientProtocol(self)
  
  def clientConnectionLost(self, connector, reason):
      "Run when connection with server is lost."
  
      #self.app.print_message("connection lost")
      Logger.debug("Lost connection: {}".format(reason.getErrorMessage()))
  
      return protocol.ReconnectingClientFactory.clientConnectionLost(
          self, connector, reason
      )
  
  def clientConnectionFailed(self, connector, reason):
      "Run when attempt to connect with server fails."
  
      #self.app.print_message("connection failed")
      Logger.debug("Connection failed. {}".format(reason.getErrorMessage()))
  
      return protocol.ReconnectingClientFactory.clientConnectionFailed(
          self, connector, reason
      )
  </code></pre>
  
  <p>Server Protocol</p>
  
  <pre><code>class CommCoreServerProtocol(LineReceiver):
  
  "Server backend to pocess the commands"
  
  def __init__(self):
  
      self._peer_host = None
      self._peer_port = None
      self._peer_repr = None
  
  def connectionMade(self):
      "Run when connection is established with server."
  
      self._peer_host = self.transport.getPeer().host
      self._peer_port = self.transport.getPeer().port
      self._peer_repr = self._peer_host + " on " + str(self._peer_port)
  
      Logger.debug(
          "Connection success! Connected to {}".format(self._peer_repr))
  
  def connectionLost(self, reason):
      "Run when connection is lost with server."
  
      Logger.error("Lost connection with peer {}".format(self._peer_repr))
  
  def lineReceived(self, line):
  
      print "REVCD LINE!", line
  
      response = self.factory.app.handle_recieved_data(line)
  
      if response:
          #self.transport.write(response)
          self.sendLine(response)
  </code></pre>
  
  <p>Server Factory</p>
  
  <pre><code>class CommCoreServerFactory(protocol.Factory):
  
  protocol = CommCoreServerProtocol
  
  def __init__(self, app):
  
      self.app = app
  </code></pre>
  
  <p>(Pardon the shoddy indent!)</p>
  
  <p>I would like to know where I may be going wrong.
  Also If you are interested I have filed this <a href="https://github.com/vaizguy/cryptikchaos/issues/1" rel="nofollow">issue</a>. If you go through my code (comm/twiscomm.py) you will see some things may not work completely on the server side especially with the handle_received_data() but this is never even called as the data is not received. </p>