2

When I try and run this (see code below) I get the "connection made" response from the server and the command prompt to write an input. However when I try and enter the input it just hangs and the server doesn't seem to receive the message. Anyone know why this is?

Thanks, please say if this isn't clear enough

Here is my chat server:

from twisted.protocols import basic



class MyChat(basic.LineReceiver):
    def connectionMade(self):
        print "Got new client!"
        self.factory.clients.append(self)

    def connectionLost(self, reason):
        print "Lost a client!"
        self.factory.clients.remove(self)

    def lineReceived(self, line):
        print "received", repr(line)
        for c in self.factory.clients:
            c.message(line)

    def message(self, message):
        self.transport.write(message + '\n')


from twisted.internet import reactor, protocol
from twisted.application import service, internet

factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []
reactor.listenTCP(8004, factory)
reactor.run()

and here is my client:

from twisted.internet import reactor, protocol


# a client protocol

class EchoClient(protocol.Protocol):

    def sendData(self):
        data = raw_input("> ")
        if data:
            print "sending %s...." % data
            self.transport.write(data)
        else:
            self.transport.loseConnection()

    def connectionMade(self):
        self.sendData()

    def dataReceived(self, data):
        print data
        self.sendData()

    def connectionLost(self, reason):
        print "connection lost"

class EchoFactory(protocol.ClientFactory):
    protocol = EchoClient

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

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


# this connects the protocol to a server runing on port 8000
def main():
    f = EchoFactory()
    reactor.connectTCP("localhost", 8004, f)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

1 Answers1

1

You made a mistake in the client. Basically, server expects to receive lines, meaning data terminated by newline. However, client sends data without newline character at the end.

So, to fix client, just add \r\n to the data:

self.transport.write(data + "\r\n")

Here is client protocol:

class EchoClient(protocol.Protocol):

    def sendData(self):
        data = raw_input("> ")
        if data:
            print "sending %s...." % data
            self.transport.write(data + "\r\n")
        else:
            self.transport.loseConnection()

    def connectionMade(self):
        self.sendData()

    def dataReceived(self, data):
        print data
        self.sendData()

    def connectionLost(self, reason):
        print "connection lost"
Dmitry Nedbaylo
  • 2,254
  • 1
  • 20
  • 20