函数getline通过套接字 [英] getline over a socket

查看:126
本文介绍了函数getline通过套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有libc的函数,会做同样的事情函数getline,而是将与所连接的插座,而不是一个FILE *流工作?

Is there a libc function that would do the same thing as getline, but would work with a connected socket instead of a FILE * stream ?

一个解决方法是调用fdopen套接字。什么是应该这样做时,应考虑的,关心的事情。是什么原因做/不这样做。

A workaround would be to call fdopen on a socket. What are things that should be taken care of, when doing so. What are reasons to do it/ not do it.

一个显而易见的理由这样做是调用函数getline和合作,但也许这是一个更好的主意来重写一些自定义的函数getline?

One obvious reason to do it is to call getline and co, but maybe it is a better idea to rewrite some custom getline ?

推荐答案

当您调用读取插座上,那么它可以pmaturely返回一个零值$ P $。
例如:

when you call a read on a socket, then it can return a zero value prematurely. eg.

 read(fd, buf, bufsize)

可以返回一个值小于BUFSIZE如果TCP套接字内核缓冲区已满。
在这样的情况下,可能需要再次调用读取功能,除非它返回零或否定的结果。

can return a value less than bufsize if the kernel buffer for the tcp socket is full. in such a case it may be required to call the read function again unless it returns a zero or a negative result.

因此​​最好避免stdio函数。你需要为了实现迭代要求越来越BUFSIZE字节可靠地读取创建读取功能包装。它应该返回零值,只有当没有更多的字节可从插座中读取,因为如果文件被从本地磁盘读取

thus it is best to avoid stdio functions. you need to create wrappers for the read function in order to implement the iterative call to read for getting bufsize bytes reliably. it should return a zero value only when no more bytes can be read from the socket, as if the file is being read from the local disk.

您可以在书中找到计算机系统的包装:程序员的角度由兰德尔科比。

you can find wrappers in the book Computer Systems: A Programmer's Perspective by Randal Bryant.

源$ C ​​$ c是提供的网站。寻找与力拓开始功能_

The source code is available at this site. look for functions beginning with rio_.

这篇关于函数getline通过套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆