使用socket使用socket发送大数据包 [英] Sending large packets using php using socket_write

查看:932
本文介绍了使用socket使用socket发送大数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像编码为base64编码并将其发送到我正在创建的C ++服务器。我使用PHP来做到这一点。



因此,PHP代码是客户端,C ++代码是监听服务器。



大图像上出现问题;例如70KB图像。它适用于小图像;例如5KB。



发生的错误是:警告: socket_write()[function.socket-write] :无法写入套接字[0]:数据报套接字上发送的消息大于内部消息缓冲区或某个其他网络限制,或者用于接收数据报的缓冲区小于数据报本身。



如果不将图像分成几个小包,可以解决这个问题吗?



我只需要发送数据包为1MB。 / p>

这是我正在使用的代码:

  $ con = file_get_contents (image url); 
$ binary_data = base64_encode($ con);
$ host =192.168.35.54;
$ port = 1060;
$ message = $ binary_data;
//无超时
set_time_limit(0);
$ socket = socket_create(AF_INET,SOCK_DGRAM,SOL_UDP)或die(无法创建socket \ n);
socket_connect($ socket,$ host,$ port)或die(无法连接到服务器\ n);
socket_write($ socket,$ message,strlen($ message))或die(无法将数据发送到server \ n);


解决方案

您可能遇到许多问题。我假设你正在使用TCP。我还假设你在一次调用socket_write时写了整个图像。我的第一个猜测是问题是接收方。当您在接收端读取套接字时,您无法保证在一次读取中获取整个数据块。通常,在读取TCP套接字时,您会循环读取并累积数据,直到获得所需的所有数据,否则会出现错误。



您可以在其他SO帖子中看到一些示例代码:



阅读来自套接字:是否保证至少获得x个字节?



编辑



在看到这些额外的细节后,我的第一个建议是切换到TCP。您可以通过这种方式执行单个发送,然后在接收端的循环中读取,如上面的示例代码中所示。否则,你将不得不打破数据包,并构建自己的错误检测代码,以确保所有部分到达,再加上顺序代码按顺序重新组装它们,这基本上只是复制了一堆功能TCP已经提供。


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 C++ code is the listening server.

The problem occurs on large images; for example 70KB images. It is working properly on small images; such as 5KB.

The error occurring is: Warning: socket_write() [function.socket-write]: unable to write to socket [0]: A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself.

Can this problem be fixed without dividing the image into several small packets?

I only need the sending packet to be 1MB.

This is the code I am using:

$con=file_get_contents("image url");
$binary_data=base64_encode($con);
$host = "192.168.35.54";
$port = 1060;
$message = $binary_data;
// No Timeout 
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die("Could not create socket\n");
socket_connect($socket, $host, $port) or die("Could not connect to server\n");
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");

解决方案

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.

这篇关于使用socket使用socket发送大数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆