2014-02-25 88 views
2

我試圖建立一個客戶端/服務器系統,客戶端發送消息到服務器。服務器只不過是打印出客戶發送的內容。從扭曲的客戶端發送多個郵件到服務器

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() 

的問題是,當我試圖從客戶端發送多個消息與此代碼,客戶提出了第一個連接後出現錯誤,併發送。

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(">") 
     if r == 'q' or len(r) == 0: break 
     sendMessage(r) 

什麼可能是錯的?這是錯誤信息。

>a 
Server said: a 
Connection lost. 
>b 
Traceback (most recent call last): 
    File "echoclient.py", line 38, in <module> 
    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 

回答

-1

爲了我自己的目的,使用套接字發送消息到服務器工作正常。

import socket 
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
client_socket.connect(('localhost', 8000)) 
while True: 
    a = raw_input("> ") 
    if a == 'q' or len(a) == 0: 
     client_socket.close() 
     break 
    else: 
     client_socket.send(a) 
+0

這個網絡代碼是越野車。你濫用'socket.send'。閱讀文檔。 –

0

反應堆不能重新啓動。

+0

然後,什麼可以用於交互式通信,需要多個消息發送之間的客戶端和服務器扭曲? – prosseek

+1

如果需要,請調用'raw_input'。它阻止並阻止你的應用程序在事件發生時處理事件,但是如果你不介意的話,它會工作得很好(並且它不會比你寫的更糟糕,而且它不會因'ReactorNotRestartable'異常而失敗)。否則,如果您想要同時處理網絡和stdio事件,請查看'twisted.internet.stdio'。 –

1

雖然這個問題很老,有兩個無關緊要的答案,但我想爲那些渴望知道的人回答。問題是,在你的服務器代碼中,你有self.transport.write(data)(它發送它接收到客戶端的每條消息),並且同時在你的客戶端代碼中,在dataReceived方法中,你有命令self.transport.loseConnection()(它一旦收到消息,就會丟失與服務器的連接)。所以,如果你刪除任何這些行,你應該沒問題。在當前設置中,客戶端發送的第一條消息將被髮送回客戶端,並且會導致連接斷開。

另外,您在每次嘗試發送消息時都調用reactor.run()。 Reactor.run只能在主函數中調用一次。

相關問題