【发布时间】:2015-08-24 11:48:23
【问题描述】:
:)
我正在尝试从我的 php 服务器中的 c# 实时应用程序接收数据,然后根据数据在浏览器中移动图片。
数据收发没有问题,但是运行代码时chrome的内存占用越来越大。
如果我在 while 循环中关闭套接字,性能会变得非常低,但内存使用会正常。所以这是关于打开的套接字...
这里是php代码:
<?php
//http://www.binarytides.com/udp-socket-programming-in-php/
//Create a UDP socket
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, "0.0.0.0" , 41181) )
{
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
//Do some communication, this loop can handle multiple clients
while(1)
{
//echo "Waiting for data ... \n";
$r = socket_recvfrom($sock, $buf, 20, 0, $remote_ip, $remote_port);
?>
<script type="text/javascript">
var data = "<?php echo $buf ?>";
</script>
<?php
}
socket_close($sock);
?>
这里是c#函数(数据发送者):
public static void SendUDP(string hostNameOrAddress, int destinationPort, string data, int count)
{
//class member : Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
//socket is defined as class member and used here
for (int i = 0; i < count; i++)
{
socket.SendTo(buffer, endPoint);
}
}
谢谢! :)
【问题讨论】:
-
你的问题到底是什么?
-
我希望内存使用稳定,而不是一直增加。
-
你是在浏览器中运行这个,还是为什么要将结果包装在一个 JS 标签中?
-
是的!我在第二行提到了“在浏览器中”。
-
我想从c# app发送数据到php服务器,然后php服务器再发送数据到浏览器。
标签: php sockets memory-leaks udp