Sending large packets using php using socket_write

前端 未结 2 421
盖世英雄少女心
盖世英雄少女心 2021-01-16 14:48

I am trying to encode an image to base64 encoding and send it to a C++ server I am creating. I am using PHP to do that.

Therefore, the PHP code is the client and the

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-16 14:57

    There are a number of possible problems you could have. I assume you're using TCP. I'm also assuming you're writing the entire image in one call to socket_write. My first guess would be that the problem is one the receiving side. When you read the socket on the receiving side, you're not guarantted to get the entire block of data in one read. Normally, when reading TCP sockets, you read in a loop and accumulate the data that way until you've gotten all the data you're expecting, or you get an error.

    You can see some example code in this other SO post:

    Read from socket: Is it guaranteed to at least get x bytes?

    EDIT

    After seeing these additional details, my first suggestion would be to switch to TCP. You can do a single send that way, and then read in a loop on the receiving side like in the example code above. Otherwise, you'll have to break up the packet, and build in your own error detection code to make sure all the pieces arrive, plus put in sequence codes to reassemble them in order, which is basically just duplicating a bunch of functionality TCP already provides.

提交回复
热议问题