I'm有問題。 從一開始,我就開發一個必須與扭曲的服務器同步的ios應用程序。我準備將信息以JSON格式發送。然後我開始發送大塊數據(現在大概是256bytes + 4 bytes
的命令 - 是的,我正在執行我自己的協議)。連接沒問題,我在我的服務器上收到這些數據包(在我自己的協議子類的dataReceived
函數中)。 ios方法:NSInteger writtenBytes =[self.outputStream write:[data bytes] maxLength:[data length]]
將寫入的字節返回到流中。對於前4個數據包,返回值是預期的(260字節)。如果我有更多可用的字節發送,下一次我調用該方法時,它會返回0(該蘋果文檔說:"If the receiver is a fixed-length stream and has reached its capacity, 0 is returned."
)。扭曲緩衝器滿中的TCP連接
所以我推斷,輸入緩衝區已滿。我不知道如何釋放緩衝區(我不知道如何到達緩衝區)。我不知道緩衝區的限制在哪裏(在我看來,這幾乎是荒謬的)。
這是服務器(只用於基於字符串中協議的基礎這一問題的重要的東西)的基本測試
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
class IphoneSync(Protocol):
def __init__(self):
self.__buffer = ""
def connectionMade(self):
self.transport.write("0:")
self.factory.clients.append(self)
print "clients are ", self.factory.clients
def connectionLost(self, reason):
self.factory.clients.remove(self)
def dataReceived(self, data):
#print "data is ", data
a = data.split(':')
if len(a) > 1:
command = a[0]
content = a[1]
msg = ""
if command == "iam":
#user&Pass checking
msg = "1"
elif command == "msg":
self.__buffer += data
msg = "1: continue"
elif command == "fin":
#procesaremos todo
#Convertir datos en json
#insertar/actualizar data en sqlite
#devolver respuesta
print "buffer is", self.__buffer
msg = "2: procesing"
print msg
self.transport.write(msg)
#for c in self.factory.clients:
#c.message(msg)
def message(self, message):
self.transport.write(message)
#self.transport.write(message + '\n')
factory = Factory()
factory.protocol = IphoneSync
factory.clients = []
dir(factory)
reactor.listenTCP(8000, factory)
print "Iphone Chat server started"
reactor.run()
只見LineReceiver
類,但我可不是發送線。傳輸的數據可能非常大(10Mb-50Mb)。我正在考慮使用Consumer/Producer模型,或RPC Protocols(AMP或PB)作爲解決方案,但我想用自己的協議工作。 如果有人知道如何幫助我,我會非常感激。不管怎麼說,還是要謝謝你。