应我的recv缓冲区是多大时,在套接字库调用接收 [英] How large should my recv buffer be when calling recv in the socket library

查看:154
本文介绍了应我的recv缓冲区是多大时,在套接字库调用接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C中的socket库的几个问题下面是code的一个片段,我会请参考我的问题。

 字符recv_buffer [3000];
的recv(插座,recv_buffer,3000,0);


  1. 我决定有多大使recv_buffer怎么办?我使用的是3000,但它的随意性。

  2. 如果的recv()收到一个数据包比我更大的缓冲区,会发生什么?

  3. 我怎么能知道我是否已经收到完整的消息,而无需再次调用接收,并让它永远等下去的时候有什么要接收?

  4. 是有办法,我可以做一个缓冲区没有一个固定的空间量,这样我就可以不断加入到它,不用担心运行的空间呢?也许使用 strcat的来连接最新的的recv()响应缓冲区?

我知道这是一个很大的一个问题,但我将不胜AP preciate任何响应。


解决方案

对这些问题的答案取决于你是否使用流套接字( SOCK_STREAM )或数据报套接字( SOCK_DGRAM ) - TCP / IP中,前者对应于TCP,后者为UDP

你怎么知道有多大使)传递给缓冲区的recv(


  • SOCK_STREAM :它并不真正的问题 - 只是选择一个尺寸(3000是罚款)。更大的缓冲区将更有效率,如果您传输大量的数据。


  • SOCK_DGRAM :使用足够大,可以容纳您的应用程序级协议发送有史以来的最大数据包缓冲区。如果你使用UDP,那么一般的应用层协议不应该比发送大约1400字节的数据包,因为他们肯定会需要分片和重组。


如果的recv 得到一个数据包大于缓冲区?

较大,会发生什么

  • SOCK_STREAM :这个问题并没有真正意义的放,因为流套接字没有包的概念 - 他们只是不断字节流。如果有更多的字节可用比你的缓冲区中读取了空间,那么他们就会被OS进行排队,并供您下次调用的recv


  • SOCK_DGRAM :多余字节被丢弃


我怎么能知道,如果我收到整个消息?


  • SOCK_STREAM :您需要建立确定结束消息到您的应用级协议的一些方法。通常这可以是一个长度preFIX(开始消息的长度的每个消息)或消息结束的定界符(它可能只是在一个基于文本的协议换行符,例如)。第三,较少使用的,选择是要求一个固定大小为每条消息。这些选项的组合也是可能的 - 例如,固定大小的报头,其包括长度值


  • SOCK_DGRAM :一个单一的的recv 调用总是返回一个数据报


有没有一种办法可以让缓冲区没有一个固定的空间量,这样我就可以不断加入到它,不用担心用完空间?

没有。但是,您可以尝试使用来调整缓冲的realloc()(如果它最初是分配的malloc()释放calloc(),这是)。

I have a few questions about the socket library in C. Here is a snippet of code I'll refer to in my questions.

char recv_buffer[3000];
recv(socket, recv_buffer, 3000, 0);

  1. How do I decide how big to make recv_buffer? I'm using 3000, but it's arbitrary.
  2. what happens if recv() receives a packet bigger than my buffer?
  3. how can I know if I have received the entire message without calling recv again and have it wait forever when there is nothing to be received?
  4. is there a way I can make a buffer not have a fixed amount of space, so that I can keep adding to it without fear of running out of space? maybe using strcat to concatenate the latest recv() response to the buffer?

I know it's a lot of questions in one, but I would greatly appreciate any responses.

解决方案

The answers to these questions vary depending on whether you are using a stream socket (SOCK_STREAM) or a datagram socket (SOCK_DGRAM) - within TCP/IP, the former corresponds to TCP and the latter to UDP.

How do you know how big to make the buffer passed to recv()?

  • SOCK_STREAM: It doesn't really matter - just pick a size (3000 is fine). Larger buffers will be more efficient if you're transferring large amounts of data.

  • SOCK_DGRAM: Use a buffer large enough to hold the biggest packet that your application-level protocol ever sends. If you're using UDP, then in general your application-level protocol shouldn't be sending packets larger than about 1400 bytes, because they'll certainly need to be fragmented and reassembled.

What happens if recv gets a packet larger than the buffer?

  • SOCK_STREAM: The question doesn't really make sense as put, because stream sockets don't have a concept of packets - they're just a continuous stream of bytes. If there's more bytes available to read than your buffer has room for, then they'll be queued by the OS and available for your next call to recv.

  • SOCK_DGRAM: The excess bytes are discarded.

How can I know if I have received the entire message?

  • SOCK_STREAM: You need to build some way of determining the end-of-message into your application-level protocol. Commonly this is either a length prefix (starting each message with the length of the message) or an end-of-message delimiter (which might just be a newline in a text-based protocol, for example). A third, lesser-used, option is to mandate a fixed size for each message. Combinations of these options are also possible - for example, a fixed-size header that includes a length value.

  • SOCK_DGRAM: An single recv call always returns a single datagram.

Is there a way I can make a buffer not have a fixed amount of space, so that I can keep adding to it without fear of running out of space?

No. However, you can try to resize the buffer using realloc() (if it was originally allocated with malloc() or calloc(), that is).

这篇关于应我的recv缓冲区是多大时,在套接字库调用接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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