Python Socket

TCPServer/Client

Tornado TCPServer

运行输出内容:

Server start ......
New connection : ('127.0.0.1', 39860) <tornado.iostream.IOStream object at 0x101e6d0>
A new user has entered the chat room. ('127.0.0.1', 39860)
connection num is: 1
User said: Hello,  ('127.0.0.1', 39860)
User said: word!  ('127.0.0.1', 39860)
New connection : ('127.0.0.1', 39861) <tornado.iostream.IOStream object at 0x101e750>
A new user has entered the chat room. ('127.0.0.1', 39861)
connection num is: 2
User said: Hello,  ('127.0.0.1', 39861)
User said: word!  ('127.0.0.1', 39861)

经测试后发现,tornado的接收方法里,已经做了粘包处理。比如第18行的代码,就是用的tornado.iostream.BaseIOStream类的read_until(delimiter, callback) 方法。这个方法,将会从缓冲区里,直到读到截止标记(比如'\n'),就产生一次回调。如果没有截止标记,缓冲区就攒数据,直到等到截止标记出现,才会产生回调。看了一下源码,缓冲区默认是max_buffer_size=104857600。

运行后,可以看到,服务器端,会打出:

input some thing,and press Enter!
> ('127.0.0.1', 39860) Hello, 
('127.0.0.1', 39860) word! 

> ('127.0.0.1', 39861) Hello, 

> ('127.0.0.1', 39861) word!