免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2397 | 回复: 2
打印 上一主题 下一主题

twisted中怎么自动把一个接收包体分隔成多个包给分别处理了? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-12 12:00 |只看该作者 |倒序浏览
学习使用twisted做简单的服务客户端,发现服务端自动把一个接收包体分隔成多个包给分别处理了,不知怎么回事,附上代码:
-------------srv.py-------------------------
#coding=utf-8
import time,string,sys
from twisted.internet.protocol import Protocol,Factory
if sys.platform == 'linux2':
    from twisted.internet import epollreactor
    epollreactor.install()
    print 'install epoll module ok!'
else:
    from twisted.internet import iocpreactor
    iocpreactor.reactor.install()
    print 'install IOCP module ok!'
from twisted.internet import reactor
class PMSFactory(Factory):
    pass      
class Echo(Protocol):
    def dataReceived(self,data):
        print "OK!"
        self.transport.write('OK')
   
f = PMSFactory()
f.protocol = Echo
reactor.listenTCP(1111,f)
reactor.run()
-----------------clnt.py--------------------
#coding=utf-8
from twisted.internet.protocol import Protocol, ClientFactory
class Echo(Protocol):
    def dataReceived(self, data):
        print "[recv]:",data
        self.transport.loseConnection()
    def connectionMade(self):
        s='a'*10240#此处如果字节数小于4096就不会出现问题
        self.transport.write(s)
   
class EchoClientFactory(ClientFactory):
    def buildProtocol(self,addr):
        return Echo()
from twisted.internet import reactor
f=reactor.connectTCP('127.0.0.1', 1111, EchoClientFactory())
reactor.run()
-----------------------------------------------
如果客户端一次发的数据超过4096个字符,服务端会截成多个接收包,并且服务端会调用多次dataReceived,
这样的话,如果客户端的请求包体确实要超为4096个,那岂不是服务端不能使用dataReceived处理了?

论坛徽章:
0
2 [报告]
发表于 2009-11-12 12:50 |只看该作者
楼上还是先去了解一下TCP的基本知识吧。 这里就说一个结论,Fragmentation不是第四层的事情。

论坛徽章:
0
3 [报告]
发表于 2009-11-14 21:08 |只看该作者
这个....
tcp上面是不分包的,是流,想怎么分是你应用层协议的事情。建议你看看twisted里面smtp协议的状态机的实现
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP