dongtu1958 2009-07-31 16:11
浏览 74
已采纳

PHP pfsockopen在一个会话中

I'm making an MSN client in PHP. I have this code, which connects to a server and logs in to MSN:

$server2 = explode(":", $xfr[3]);
$socket2 = pfsockopen($server2[0], (int)$server2[1]);
$_SESSION["socket"] = $socket;
echo '<b>Connected to 2nd server.</b><br />';
//Send MSNP version
fputs($socket2, "VER 0 MSNP10 CVR0
");
echo fread($socket2, 5000) . '<br />';
//Send user-agent
fputs($socket2, "CVR 1 0x0409 php ".phpversion()." i386 MSNMSGR 7.0.0000 MSMSGS ".$_POST["username"]."
");
echo fread($socket2, 5000) . '<br />';
//Send username
fputs($socket2, "USR 2 TWN I ".$_POST["username"]."
");
//Read USR
$usr = fread($socket2, 5000);
echo $usr . '<br />';
$usr = explode(" ", $usr);//This is for later usage.

Now I need to use this socket in another page (AJAX/status.php). php.net says that the connection stays available. However, this is status.php (just ignore $_SESSION["cid"]), which is called via AJAX:

<?php
session_start();
fputs($_SESSION["socket"], "CHG 12 " . $_GET["s"] . " " . $_SESSION["cid"], 5000);
echo fread($_SESSION["socket"]);

Which should change the status. I get this error:

<br />
<b>Warning</b>:  fputs(): supplied argument is not a valid stream resource in <b>C:\wamp\apps\msnphp\AJAX\status.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>:  Wrong parameter count for fread() in <b>C:\wamp\apps\msnphp\AJAX\status.php</b> on line <b>4</b><br />

My socket is persistent, and it's id is saved in session variable $_SESSION["socket"]. I do not understand why it does not work.

I use Windows XP Professional SP2 as server, with WAMP (Apache, MySQL and PHP) server.

Can anyone help me? Thanks!

  • 写回答

4条回答 默认 最新

  • dongmiao260399 2009-07-31 16:14
    关注

    pfsockopen() returns a resource. You cannot store resources in the Session as they are just handles to external resources which may not be there later.

    If you request the same page again you might get to reuse the connection by calling pfsockopen() again with the same parameters, but I don't think you have any guarantee of this, and it probably won't be practical as for this you probably want one connection per user session.

    You could start background PHP processes which connect to the remote server, and read/write events into a queue (maybe a database or memcached). You'd have to make sure these processes are terminated properly otherwise you could quickly have a lot sitting there. Your front-end PHP script can then just read/write from/to the queue.

    The problem you have is really based on HTTP being stateless, but the service you are connecting to being stateful. So you have to somehow maintain state (for the external resource) on your webserver, which is not something that is very easy to do with PHP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算