dstobkpm908182 2015-02-08 14:55
浏览 33

制作套接字服务器时CLI输出中的奇怪字符

I am trying to test a Client-Server communication using Socket programming in PHP. After trying some samples given on PHP Reference site, the communication is working OK.

But, the problem is that I am getting some bogus or invalid characters in the buffer.

php sockets invalid characters http://i60.tinypic.com/oqwyz4.png

Of course, I am able to vanish them using Regex, but it would be more nice to know the reason behind this.

I think that this person is also having the same problem.

Source Code of Server:

error_reporting(E_ALL);

set_time_limit(0);

$address = "tcp://192.168.1.60:9160";

$sock = stream_socket_server($address, $errcode, $errstring);

if (!$sock) {
    echo $errcode.": ".$errstring."
";
}
else {
    do {
        while ($client = stream_socket_accept($sock)) {
            fwrite($client, "*** Welcome ***".PHP_EOL);
            $input = trim(fgets($client, 2048));
            if (!empty($input)) {
                fwrite($client, "Server: ".$input.PHP_EOL);
            }
            else {
                fwrite($client, "--- NO INPUT ---".PHP_EOL);
            }
            echo trim($input).PHP_EOL;
            if ($fp = fopen("log.txt", "a+")) {
                fwrite($fp, trim($input).PHP_EOL);
                fclose($fp);
            }
            fclose($client);
        }
    } while (true);
}

fclose($sock);

Any help would be appreciated :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀