I wanted to know how to flush the socket streams while doing socket programming in C. I tried all the options- setting TCP_NODELAY
using the following code-
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
Note: all the flag
and sockfd
are declared correctly.
I used this function both before send(
) and after send()
but it did not make any difference.
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(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.
If you need to force data to be sent immediately, however, flushing the buffer will send any data which has not yet been sent. Note that when you close a socket, it automatically flushes any remaning data, so there's no need to flush before you close.
close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed.
The stream socket defines a reliable connection service. Data is sent without error or duplication and is received in the order sent. Flow control is built in to avoid data overruns. No boundaries are imposed on the data; the data is treated as a stream of bytes.
The socket() function shall create an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets. The socket() function takes the following arguments: domain. Specifies the communications domain in which a socket is to be created.
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.
Have you used wireshark or something to see what's going behind the scenes when you set TCP_NODELAY?
You might want to read The ultimate SO_LINGER page, or: why is my tcp not reliable, which I think applies to your situation.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With