估计是网速问题如果上半部显示通信内容,下半部发送消息。这相当与一个聊天室
如果用ajax和myslq,应该很快的。

解决方案 »

  1.   

    PHP 做长链接用Socket通信 那么PHP本身运行在哪儿呢?是服务器、还是客户端,用HTTP协议吗
      

  2.   

    To:fxs_2008 
    如果用ajax和myslq,应该很快的。  因为要保持长连接 AJAX怕是不方便,代码部分和现在这差不了多少,一会贴上完整的。To:Mistruster 
    PHP本身运行在哪儿呢?是服务器、还是客户端,用HTTP协议吗PHP运行在APACHE里 MOD的方式,对SOCKET服务器来说,是客户端,和SOCKET服务器通过SOCKET通信,和最终用户用HTTP。
      

  3.   

    function.php<?php
    function format_time(){
    list($micro,$second) = explode(' ',microtime());
    return date('H:i:s', $second).substr($micro,1,4);
    }function wlog($content){
    $file = 'log/'.date('Ymd').'.log';
    $handle = fopen($file,'ab');
    fwrite($handle, format_time().' -- ' . $content . "\n" );
    fclose($handle);
    }
    ?>
    save.php<?php
    include('function.php');
    wlog('begin write');
    if(isset($_POST['content'])){
    $content = ($_POST['content']) ? $_POST['content'] : ' ';
    $file = 'announce/1234.txt';

    $handle = fopen($file, 'ab');
    fwrite($handle, format_time().'|'.$content."\n");
    fclose($handle);
    echo '1';
    }else{
    echo '0';
    }
    wlog('end writing ' . $content);
    ?>
    content.php<?php
    set_time_limit(0);
    ob_implicit_flush(true);
    include('function.php');
    $server = '192.168.1.200';
    $port = 7082;$fp = fsockopen($server, $port);fgets($fp,2048);
    echo fgets($fp,2048),'<br />';
    fwrite($fp, "N\r\n");echo fgets($fp,2048),'<br />';
    fwrite($fp,"1234\r\n");echo fgets($fp,2048),'<br />';
    fwrite($fp,"123456\r\n");echo fgets($fp,2048),'<br />';
    fwrite($fp,"y\r\n");$i = 0;
    $flag = true;
    $flag2 = true;
    $handle = 0;
    while($flag){
    if ($i % 5 == 0){
    wlog('begin get');
    $data = fgets($fp,2048);
    if (strlen($data) == 0){
    break;
    }
    echo format_time().'--'.strlen($data).'--'.$data.'<br />';
    ob_flush();
    wlog('begin read');
    $content = check_file();
    wlog('end reading ' . $temp);
    if ($content == 'quit'){
    while($falg){
    echo fgets($fp,2048),'<br />';
    ob_flush();
    if (strlen(fgets($fp,2048)) <= 2){
    $flag = false;
    $flag2 = false;
    }
    }
    }elseif ($content != ''){
    wlog('begin write socket');
    fwrite($fp,$content."\r\n");
    wlog('end write socket');
    }
    }
    $i ++;
    wlog('end getting '.$data);
    }
    function check_file(){
    //wlog('begin read');
    global $handle;
    $file = 'announce/1234.txt';
    $contents = file($file);
    $temp = $contents[$handle];
    if($temp){
    $handle ++;
    $temp_item = explode('|',$temp);
    $return = $temp_item[1];
    }else{
    $return = '';
    }
    //wlog('end reading ' . $temp);
    return $return;
    }?>
    post.php<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <title>发言</title>
    <link href="styles/default.css" rel="stylesheet" type="text/css" />
    <script language="javascript" src="jquery.js"></script>
    <script language="javascript">
    $(document).ready(function(){
    $('#announce').submit(function(){
    $.post('save.php',{content: $('#content').val()});
    $('#content').val('');
    return false;
    });
    });
    </script>
    </head><body onload="document.getElementById('content').focus()">
    <form name="announce" id="announce" method="post" action="post.php">
    <input type="text" name="content" id="content" /> <input type="submit" id="submit" value="Go" />
    </form>
    </body>
    </html>
    index.php<?php
    $file = 'announce/1234.txt';
    $handle = fopen($file, 'wb');
    fclose($handle);$file = 'log/'.date('Ymd').'.log';
    $handle = fopen($file, 'wb');
    fclose($handle);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <title>PHP Mud</title>
    </head><frameset rows="*,30" frameborder="no" border="0" framespacing="0">
        <frame src="content.php" name="mainFrame" id="mainFrame" title="mainFrame" />
        <frame src="post.php" name="bottomFrame" scrolling="No" noresize="noresize" id="bottomFrame" title="bottomFrame" />
    </frameset>
    <noframes><body>
    </body>
    </noframes></html>
      

  4.   

    从描述上感觉这个跟聊天室差不多
    http://news.newhua.com/news1/program_database/2008/418/084189221726H07JF60A0DF46BF4D45792EIB0BJH77A8GIH11EEH00_2.html?lt=common
      

  5.   

    http://topic.csdn.net/u/20080619/10/88cd2297-5093-4b94-a500-19bdb8c54ec3.html
    雪候鸟的文章测试不了,win不支持百万级秒,也不支持强制不缓存另外,本地端口也不好测
      

  6.   

    上面的错了
    http://blog.csdn.net/laruence/archive/2008/04/16/2298617.aspx
      

  7.   

    http://blog.csdn.net/laruence/archive/2008/04/16/2298617.aspx
      

  8.   

    顺便请问一下:
    那个ip和端口有何作用?代表什么?web服务器?
      

  9.   

    问题可能是循环读写SOCKET引起的,和文件应该无关,因为我用MYSQL 共享内存都试过了,。