C语言 在 C 套接字编程中刷新套接字流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2171412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flushing the socket streams in C socket programming
提问by MohamedSanaulla
I wanted to know how to flush the socket streams while doing socket programming in C. I tried all the options- setting TCP_NODELAYusing the following code-
我想知道如何在用 C 进行套接字编程时刷新套接字流。我尝试了所有选项-TCP_NODELAY使用以下代码设置-
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
Note: all the flagand sockfdare declared correctly.
注意:所有flag和sockfd都正确声明。
I used this function both before send() and after send()but it did not make any difference.
我在send()之前和之后都使用过这个函数,send()但它没有任何区别。
Also someone had suggested to use shutdown()after each send()but that works only for one instance. When I use it to send some text again, it doesn't work- actually the connection gets closed after I use shutdown().
也有人建议shutdown()在每个之后使用,send()但这仅适用于一个实例。当我再次使用它发送一些文本时,它不起作用 - 实际上在我使用shutdown().
shutdown(sockfd, SHUT_WR);
Can someone help in this regard?
有人可以在这方面提供帮助吗?
I wanted to added that- the server is a Java socket and the client is a C socket. The C socket implements the JVMTI interface and sends the information to the Java socket.
我想补充一点 - 服务器是一个 Java 套接字,客户端是一个 C 套接字。C 套接字实现 JVMTI 接口并将信息发送到 Java 套接字。
采纳答案by Nikolai Fetissov
You might want to read The ultimate SO_LINGER page, or: why is my tcp not reliable, which I think applies to your situation.
您可能想阅读最终的 SO_LINGER 页面,或者:为什么我的 tcp 不可靠,我认为这适用于您的情况。
Edit:
编辑:
Calling send()on a TCP socket multiple times is not that uncommon :) It's normal usage, I mean. You probably have issues on the server side, where server expects certain number of bytes and blocks waiting for it.
As far as I know JVM TI does not dictate any over-the-wire protocol, so you'll have to come up with your own. Define structure of the record the client sends and server expects, put data length in there if size varies. You might also implement some application-level acknowledgement from server back to client.
send()多次调用TCP 套接字并不少见 :) 这是正常使用,我的意思是。您可能在服务器端遇到问题,服务器需要一定数量的字节和块等待它。
据我所知,JVM TI 并没有规定任何在线协议,所以你必须想出你自己的协议。定义客户端发送和服务器期望的记录结构,如果大小不同,则将数据长度放在那里。您还可以实现一些从服务器返回到客户端的应用程序级确认。
回答by Tony The Lion
Doing a bit of research on Google, it doesn't seem like there's a way to flush a socket stream explicitly. You can set the TCP_NODELAY and then it will turn off the TCP Nagle algorithm. This should make sure that the data gets sent immediately and not wait until it's buffer is full before sending.
在谷歌上做一些研究,似乎没有办法显式刷新套接字流。您可以设置 TCP_NODELAY,然后它将关闭 TCP Nagle 算法。这应该确保数据立即发送,而不是等到缓冲区满后再发送。
Have you used wireshark or something to see what's going behind the scenes when you set TCP_NODELAY?
当你设置 TCP_NODELAY 时,你是否使用过wireshark 或其他东西来查看幕后发生了什么?
回答by Axel Ernesto Axel
just use fdopen. In Linux anything is a file. eg.
只需使用 fdopen。在 Linux 中,任何东西都是文件。例如。
FILE *f = fdopen(socketdescriptor, "w+");
. . .
n = write(socketdescriptor, "this is a message", 17);
fflush(f);
...
回答by Timothy Baldwin
You can't explicitly flush as socket as the data is always sent as soon reasonably possible, taking into account the available buffer space at the receiver and the need to avoid network congestion. Setting TCP_NODELAY permits the sending of packets less than maximum size whilst there are still outstanding acknowledgements.
考虑到接收器的可用缓冲区空间和避免网络拥塞的需要,您不能明确地将数据刷新为套接字,因为数据总是尽可能快地发送。设置 TCP_NODELAY 允许在仍有未完成的确认时发送小于最大大小的数据包。
回答by Alexander Farber
socket-unix-faq says that you can't flush sockets
socket-unix-faq 说你不能冲洗套接字

