Sockets - Prevent Partial Writes
I have written a socket client program in C++. At some point, I have to send data, and this is how I do it:
lenSent = write(serverSocket,sendBuf, len); I can use send() instead of write(). It makes no difference to this question. I have set it to be non-blocking, because the thread must not get stuck. Unfortunately, it is connecting to a server which exits if I do a partial write to the socket. How can I prevent a non-blocking socket from doing a partial write? Is there some way to poll it and make sure that it can handle the number of bytes I wish to send? Thanks. Brandon |
For that, you can use 'select'. You will find useful info in manpages:
Code:
man send http://beej.us/guide/bgnet/output/ht...age/index.html http://beej.us/guide/bgnet/output/ht...ed.html#select |
The man page for send says:
"The select(2) call may be used to determine when it is possible to send more data." This isn't really enough for me to know how to do it. I have never used select before. |
Have you read this: http://beej.us/guide/bgnet/output/ht...d.html#sendall ?
Here is an example: Code:
/* |
Thanks! Does select do the write itself?
|
Quote:
|
Thanks again. I am also experimenting with checking to see how many unsent bytes are in the socket's internal buffer and waiting until it reaches zero with:
int error, value; for (.....value > 0.....) error = ioctl(serverSocket, SIOCOUTQ, &value); Brandon |
All times are GMT -5. The time now is 06:21 AM. |