- [Winsock] Is it sure that the buffer is send by one send() call?
- Posted by gany on August 10th, 2004
Hi @ all.
I connect to HTTP webserver. I want to retrieve a website with this
command-string:
const char* command = "GET test.html HTTP/1.1\r\n\r\n";
Is it okay if i just do
send(command, strlen(command));
Or could it happen that only a part of the command-string is sent and I
need to call send again and again (in a loop) until the buffer is sent
completly? That would be really ugly.
I ask this question because of this part of the documenation of send:
"If no error occurs, send returns the total number of bytes sent, *which
can be less than the number indicated by len.*"
I'm working with blocking sockets.
- Posted by mark greenlancer on August 10th, 2004
gany wrote:
the string will be send completely and the function returns
when the string is out.
On non-blocking sockets:
If you call send and a previous send is just working send
returns an error.
always check your return values!
mark
- Posted by Charlie Gibbs on August 10th, 2004
In article <cfalht$6sv$04$1@news.t-online.com>, gany@hotmail.com (gany)
writes:
That's right. There is no guarantee that send() will send the entire
string; underlying systems might not be able to take it all at once.
But it's not "really ugly", you just have to set up a loop to keep
trying until everything gets sent. Something like this does the trick:
s = buf; /* Data to send */
bytesleft = strlen (buf); /* Bytes left to send */
while (bytesleft > 0) {
i = send (sockfd, s, bytesleft, 0); /* Send what we can. */
if (i >= 0) {
bytesleft -= i; /* Number of bytes left */
s += i; /* Bump buffer pointer. */
continue; /* Try to send the rest. */
}
/* This code is necessary only if the socket is non-blocking.
You might want to add some code to check for time-outs. */
if (WSAGetLastError () == WSAEWOULDBLOCK)
continue;
/* If we get here, send() had an error of some sort. */
...
}
--
/~\ cgibbs@kltpzyxm.invalid (Charlie Gibbs)
\ / I'm really at ac.dekanfrus if you read it the right way.
X Top-posted messages will probably be ignored. See RFC1855.
/ \ HTML will DEFINITELY be ignored. Join the ASCII ribbon campaign!