【发布时间】:2014-01-07 02:07:51
【问题描述】:
当服务器和客户端都位于不同的终端窗口时,我无法让它们进行通信。我可以让它们都连接,但实际上并没有将它们的输出发送到彼此的 windows.I 客户:
from twisted.internet import reactor, stdio, protocol
from twisted.protocols import basic
class Echo(basic.LineReceiver):
def connectionMade(self):
print "Welcome to the Chat, you have now connected"
def lineReceived(self, line):
self.sendLine(line)
if line=="exit":
connectionLost()
def connectionLost(self):
self.transport.loseConnection()
class EchoClientFactory(protocol.ClientFactory):
protocol = Echo
factory = EchoClientFactory()
reactor.connectTCP("localhost", ...., factory)
reactor.run()
服务器:
from twisted.internet import reactor, protocol, stdio
from twisted.protocols import basic
class Echo(basic.LineReceiver):
print "Welcome to Chat"
def connectionMade(self):
print "A new client has connected"
def lineReceived(self, line):
self.sendLine(line)
if line=="exit":
connectionLost()
def connectionLost(self):
self.transport.loseConnection()
class EchoServerFactory(protocol.ServerFactory):
protocol = Echo
factory = EchoServerFactory()
reactor.listenTCP(...., factory)
reactor.run()
【问题讨论】: