Sending lots of data over a TCP socket
Posted: Mon Dec 15, 2003 2:09 am
I have a PHP page that sends some data through a TCP socket to another process on the web server. The amount of data can vary from 1 KB to about 10 MB. I know that a fwrite () call on a socket could return even if less than the requested amount has been transmitted (due to buffer availability on the TCP stack and the receiving end). I also know that a C function to send a block of data of a specified length looks like this:
I need some help to translate the above function to PHP. So far what I have is this:
This might work, but it is really memory inefficient for large buffers, since all data is copied several times over and over until it is transmitted.
Can anyone suggest a more efficient way to "advance" in a string and use it as a parameter to fwrite ?
Code: Select all
BOOL sendbuf (int socket, char *buf, int len)
{
int sent;
while (len > 0)
{
sent = send (socket, buf, len, 0 /* flags */);
if (sent == -1)
return (FALSE);
len = len - sent;
buf = buf + sent;
}
return (TRUE);
}Code: Select all
function sendbuf ($socket, $buffer, $len)
{
while ($len > 0)
{
$sent = fwrite ($socket, $buffer, $len);
//error checking omitted for clairty
$len = $len - $sent;
$buffer = substr ($buffer, $sent); //this is a really bad way to skip over transmitted bytes
}
}Can anyone suggest a more efficient way to "advance" in a string and use it as a parameter to fwrite ?