socket 编程 recv 调用能否每次最多读一行?

2013-12-09 17:56:23 +08:00
 mantianyu
When communicating with serial port, I can set the serial port in Canonical Mode so that each read() call will return util a line comes, and at most one line.

assumes the line separator is \r\n.

Does TCP/IP communication support this feature?

I know that TCP/IP is based on stream and will take 'a','b','c' and '\r','\n' all the same, but I really don't want to parse the message char by char to find '\r','\n' in application.

I hope the TCP can do it for me...

any idea?
3066 次点击
所在节点    程序员
7 条回复
binux
2013-12-09 17:58:18 +08:00
do it yourself
bigeagle
2013-12-09 19:28:16 +08:00
This is definitely application logic, which TCP should not handle.

So keep calm and implement it yourself.
VYSE
2013-12-09 19:45:09 +08:00
socket.makefile
then readline
rrfeng
2013-12-09 19:49:47 +08:00
while ‘\r\n’ not in data
data+=recv()
mantianyu
2013-12-09 20:19:12 +08:00
@binux
@bigeagle
@VYSE
@rrfeng

谢谢大家^_^
sgissb1
2013-12-09 21:45:25 +08:00
ni hao !

ni wei shen me zhe me diao ?

ni wei shen me yao mei ci du yi hang?
bombless
2013-12-10 13:27:37 +08:00
噗,肯定要缓存结果的,TCP是8位流又不是数据包。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/92429

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX