Sending large packets using php using socket_write

筅森魡賤 提交于 2019-12-01 13:27:53
Robert S. Barnes

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.

You need to set the socket send buffer to be at least as large as the largest UDP datagram you are sending.

Don't ask me how to do that in PHP but at the Sockets API level it is setsockopt() with the SO_SNDBUF option.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!