C:使用send()发送长字符串
我正在使用 Linux 并尝试通过 send() 发送一条长消息。该消息为 1270 字节,但我的客户端应用程序仅接收 1024 字节。
由于 1024 字节是一个很方便的数字,我猜测 send() 一次只能发送 1024 字节。 我查看了发送的手册页,但它关于长消息的所有内容都是:
当消息无法放入套接字的发送缓冲区时,send() 通常会阻塞,除非套接字已置于非阻塞 I/O 中 模式。在非阻塞模式下,它将失败并出现错误 EAGAIN 或 EWOULD- 在这种情况下阻止。 select(2) 调用可用于确定何时 可以发送更多数据。
我使用的是阻塞模式,手册页没有说明要做什么。 我发送的确切调用如下所示: send(socket, message, strlen(message), 0);
我需要将字符串分成 1024 字节块并单独发送吗?我的客户将如何处理这个问题?如果我的客户需要做任何事情,我只会提到它是用 Java 编写的,并且它使用 InputStreamReader 来接收数据。
I'm using Linux and trying to send a long message through send(). The message is 1270 bytes, but my client application is only receiving 1024 bytes.
Since 1024 bytes is such a convenient number, I'm guessing that send() can only send 1024 bytes at a time.
I looked at the man page for send, but all it says about long messages is:
When the message does not fit into the send buffer of the socket, send()
normally blocks, unless the socket has been placed in nonblocking I/O
mode. In nonblocking mode it would fail with the error EAGAIN or EWOULD-
BLOCK in this case. The select(2) call may be used to determine when it
is possible to send more data.
I'm using blocking mode, and the man page doesn't say what to do.
My exact call to send looks like this:send(socket, message, strlen(message), 0);
Would I need to split up the string into 1024 byte chunks and send them separately? And how would my client handle this? If my client needs to do anything, I'll just mention that it's in Java and it uses InputStreamReader to receive data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对
send
的实际工作原理存在误解。它不会发送您向其发送的任何内容,它最多发送您作为参数传入的字节数。这意味着您必须检查send
的返回值,删除从发送队列中实际发送的字节数,并尝试通过另一个发送调用发送剩余的内容。顺便说一句,对于recv
来说也是如此。There is a misunderstanding on your part as to how
send
actually works. It does not send whatever you throw at it, it sends at most the number of bytes you pass in as a parameter. That means you have to check the return value ofsend
, remove the number of bytes that actually got send from your send queue and try to send the remaining stuff with another call to send. Same holds true forrecv
, by the way.除了 Jim Brissom 所说的之外,还值得指出SOCK_STREAM 套接字(即 TCP)没有消息边界的概念 - 连接只是一个长的非结构化字节流。
仅仅因为您通过一次
send()
调用发送了 100 个字节(比如说),不就意味着它会在一次recv()
调用中以 100 字节的形式到达。在极端情况下,它可能以 100 个单独的recv()
形式到达,每个 1 字节 - 或者在另一个方面,它可能作为更大recv()
的一部分到达,以及之前或之后的数据。您的接收者必须能够处理这个问题 - 如果您需要在流中定义单独的消息,您必须在应用程序层自行将某种结构强加于流上。
In addition to what Jim Brissom said, it is worth pointing out that
SOCK_STREAM
sockets (ie. TCP) do not have a concept of message boundaries - the connection is just one long unstructured stream of bytes.Just because you sent 100 bytes (say) with one
send()
call does not mean it will arrive as 100 bytes onerecv()
call. In the extreme, it may arrive in 100 separaterecv()
s of 1 byte each - or at the other, it may arrive as part of a largerrecv()
, along with prior or following data.Your receiver must be able to handle this - if you need to define individual messages within the stream, you must impose some sort of structure upon the stream yourself, at the application layer.