简体   繁体   English

Unix套接字发送接收客户端到服务器的大块数据错误

[英]Unix socket send receive client to server big chunk of data error

I have problem with client(php)-server(c++) communications with big amount of data. 我有大量数据的client(php)-server(c++)通信问题。 Server(c++) can not receive from client (php) big chunk of data. 服务器(c ++)无法从客户端(php)接收大量数据。 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). 当客户端发送少量信息时,一切正常,但是当我尝试发送大数据块(即〜8kb)时,我断开了通信连接(由于它是后台进程,所以不知道接收到什么服务器)。

Please, receive following code that I use and correct it if it's possible: 请接收以下我使用的代码,并在可能的情况下予以更正:

PHP part after connect (it's send $sendbuf ): 连接后的PHP部分(发送$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: C ++部分:

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;
}
}

Your receiving C-Code seems to make the following assumption. 您收到的C代码似乎有以下假设。

  • Data arrives in chunks of 1024 or less 数据以1024个或更少的块的形式到达
  • Each chunk is terminated by a magical 26 每个组块都以魔法26结尾
  • end of communication occurs if a chunk is smaller then 1024 如果块小于1024,则发生通信结束

This however does not have to hold true. 但是,这不必成立。 The OS is free to supply you with as little data as it likes on your recv(...) call even if there are still large chunks of data in the stream waiting for you. 操作系统可以自由地在您的recv(...)调用中向您提供尽可能少的数据,即使流中仍有大量数据在等待您。 This simply becomes more likely the higher the volume. 音量越高,这种可能性就越大。

Once you receive less then 1024 byte the server closes the connection and the PHP script sees a write error. 一旦收到少于1024字节的内容,服务器将关闭连接,PHP脚本将看到写入错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM