V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
mantianyu
V2EX  ›  程序员

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

  •  
  •   mantianyu · 2013-12-09 17:56:23 +08:00 · 3088 次点击
    这是一个创建于 4027 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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?
    7 条回复    1970-01-01 08:00:00 +08:00
    binux
        1
    binux  
       2013-12-09 17:58:18 +08:00
    do it yourself
    bigeagle
        2
    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
        3
    VYSE  
       2013-12-09 19:45:09 +08:00
    socket.makefile
    then readline
    rrfeng
        4
    rrfeng  
       2013-12-09 19:49:47 +08:00
    while ‘\r\n’ not in data
    data+=recv()
    mantianyu
        5
    mantianyu  
    OP
       2013-12-09 20:19:12 +08:00
    @binux
    @bigeagle
    @VYSE
    @rrfeng

    谢谢大家^_^
    sgissb1
        6
    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
        7
    bombless  
       2013-12-10 13:27:37 +08:00
    噗,肯定要缓存结果的,TCP是8位流又不是数据包。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1524 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:05 · PVG 01:05 · LAX 09:05 · JFK 12:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.