I have problem with client(php)-server(c++)
communications with big amount of data. Server(c++) can not receive from client (php) big chunk of data. All work fine when client send small amount of information, but when I try to send big chunk (ie. ~8kb), I got disconnection of communication (does not know what server receive because it's background process).
Please, receive following code that I use and correct it if it's possible:
PHP part after connect (it's send $sendbuf
):
socket_set_nonblock($fd);
$b_time = 20;
$b_tries= 0;
while($b_time)
{
if (($sc=@socket_connect($fd, $file))==FALSE && $b_tries > $b_time)
{
@socket_close($fd);
return false;
}
else if ($sc == true)
{
break;
}
$b_tries++;
}
$b_time=time();
while($b_time)
{
$wd=array();
array_push($wd,$fd);
if (false===($num = @socket_select($rd=NULL, $wd, $er=NULL, $select_timeout)))
{
@socket_close($fd);
return $result;
}
else
{
if ($num > 0)
{
if (false===($out_now=@socket_write($fd, $sendbuf, strlen($sendbuf))))
{
@socket_close($fd);
return false;
}
$b_time=$tm;
$sendbuf=substr($sendbuf,$out_now);
if (!strlen($sendbuf)) break;
}
else if ((time()-$b_time)>$timeout_write)
{
@socket_close($fd);
return false;
}
}
}
// then receive answer from server if ok, but no answer.
C++ PART:
int Socket::receive ( std::string& s ) const
{
if ( current_socket < 0 ) return false; // error socket failed!
char buf [1025];
memset(buf, 0, 1025);
// cleanup string
s = "";
int done = 0;
int status_or_nbytes = 0;
char* pch;
do {
status_or_nbytes = ::recv ( current_socket, buf, 1024, 0 );
if ( status_or_nbytes == 1024 )
{ // not all message readed, instead last byte is not SPECIAL_CHAR
if ( buf[1023] == 26 )
{
//std::cout << "S 26! " << std::endl;
buf[1023] = '\0';
done = 1;
}
s += buf;
}
else if ( status_or_nbytes > 0 ) // < 1024
{
if ( buf[status_or_nbytes-1] == 26 )
{
buf[status_or_nbytes-1] = '\0';
done = 1;
}
s += buf;
}
if (status_or_nbytes <= 0)
{
done = 1;
}
} while (!done);
if ( status_or_nbytes == -1 )
{
std::cout << "ERROR1" << std::endl;
return 0;
}
else
{
return 1;
}
}