在另外一个客户端执行 php s.php后, 通过nc -lU /tmp/tg.sck 建立的unix domain socket 有接收到消息。

<?php
require 'vendor/autoload.php'; use Monolog\Logger;
use Monolog\Handler\SocketHandler; // Create the logger
$logger = new Logger('my_logger'); // Create the handler
$handler = new SocketHandler('unix:///tmp/tg.sck');
$handler->setPersistent(true); // Now add the handler
$logger->pushHandler($handler, Logger::DEBUG); // You can now use your logger
$logger->info('My logger is now ready');

  

--------------------------------------------------------------------------

php (or python) listen to unix domain stream socket

I need to create a script that listens to a unix socket and forward the incoming stream to a bot. The scripts are unable to connect. The issues seems to be related to the order of things.

Proof of case

I have created a socket and I am able to write to it. In session 1, I create a listener connection

nc -lU /tmp/tg.sck

In session 2, I write to the socket.

 while true; do echo "hello"; sleep 2; done | nc -U /tmp/tg.sck

The above only works if I do it in that order. ==> Writing before you have a listener results in an error.

Using scripts (does not work)

When I replace the listing process with a PHP (or Python) script, the connection is refused because the socket is not opened.

$ python test.py
Connecting...
socket.error: [Errno 111] Connection refused

or

$ php test.php
Warning: fsockopen(): unable to connect to unix:///tmp/tg.sck:-1 (Connection refused)

Changing the order of things

If I start a working listener using the command nc -lU /tmp/tg.sck then the script does not die, but the writer process does.

Listener scripts

import socket
import sys server_address = '/tmp/tg.sck' # Analogous to TCP (address, port) pair
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(server_address)
sock.recv(512)

and the php script

$fp = fsockopen('unix:///tmp/tg.sck', -1, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
while (!feof($fp)) {
echo fgets($fp, 4096);
}
fclose($fp);
}

php monolog 的写日志到unix domain socket 测试终于成功的更多相关文章

  1. 由一个简单需求到Linux环境下的syslog、unix domain socket

    本文记录了因为一个简单的日志需求,继而对linux环境下syslog.rsyslog.unix domain socket的学习.本文关注使用层面,并不涉及rsyslog的实现原理,感兴趣的读者可以参 ...

  2. Unix Domain Socket 域套接字实现

    主要注意流程: STREAM SOCKET: Server :  socket() --->  bind() ---> listen()  ---> accept() Client: ...

  3. nginx、php-fpm默认配置与性能–TCP socket还是unix domain socket【转】

    原文地址:https://www.cnxct.com/default-configuration-and-performance-of-nginx-phpfpm-and-tcp-socket-or-u ...

  4. 【转】PHP实现系统编程(四)--- 本地套接字(Unix Domain Socket)

    原文:http://blog.csdn.net/zhang197093/article/details/78143687?locationNum=6&fps=1 --------------- ...

  5. (unix domain socket)使用udp发送>=128K的消息会报ENOBUFS的错误

    一个困扰我两天的问题, Google和Baidu没有找到解决方法! 此文为记录这个问题,并给出原因和解决方法. 1.Unix domain socket简介 unix域协议并不是一个实际的协议族,而是 ...

  6. Linux下的IPC-UNIX Domain Socket【转】

    本文转载自:http://blog.csdn.net/guxch/article/details/7041052 一. 概述 UNIX Domain Socket是在socket架构上发展起来的用于同 ...

  7. [apue] 作为 daemon, 启动 Unix Domain Socket 侦听失败?

    前段时间写一个传递文件句柄的小 demo,有 server 端.有 client 端,之间通过 Unix Domain Socket 通讯. 在普通模式下,双方可以正常建立连接,当server端作为d ...

  8. Envoy 基础教程:使用 Unix Domain Socket(UDS) 与上游集群通信

    Envoy Proxy 在大多数情况下都是作为 Sidecar 与应用部署在同一网络环境中,每个应用只需要与 Envoy(localhost)交互,不需要知道其他服务的地址.然而这并不是 Envoy ...

  9. monitor a local unix domain socket like tcpdump

    Can I monitor a local unix domain socket like tcpdump? - Super User https://superuser.com/questions/ ...

随机推荐

  1. Python3简明教程(一)—— 开始Python之旅

    第一个Python程序 作为我们第一个Python程序——打印"Hello  World!". 在终端输入Python3进入交互界面: 输入print("Hello  W ...

  2. 使用snapshot继续训练网络

    注意:snapshots和weights不能同时使用 用预训练模型进行finetune是以下命令: ./build/tools/caffe train --solver=examples/XXX/le ...

  3. CWnd::Updata的作用

    CWnd::Updata的作用 CWnd::UpdateData 调用此成员函数以在对话框中初始化数据,或者取回和验证对话框数据. BOOL UpdateData(BOOL bSaveAndValid ...

  4. 初见Vue

    一.What 官方定义:是一套用于构建用户界面的渐进式框架.这,what?不明觉厉,我反正现在还是不知道,在这之前,就只知道Vue.js是用来渲染数据的,其实它的核心库只关注视图层.不多说,用多了就知 ...

  5. mysql单实例多库与多实例单库

    一.单实例多库: 一个mysql实例,创建多个数据目录. 规划: 实例路径:/usr/local/mysql 数据目录路径: (1)/usr/local/mysql/data (2)/usr/loca ...

  6. nginx 配置虚拟机 支持pathinfo

    server { server_name shopx.local *.shopx.local; charset utf-8; root /Users/x/www/php/shopx.local/sho ...

  7. @RestController 与 @Controller 注解区别

    文章来源:https://www.cnblogs.com/hello-tl/p/9202658.html @RestController注解相当于@ResponseBody + @Controller ...

  8. 【HDU 6153】A Secret (KMP)

    Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...

  9. POJ 1252 Euro Efficiency(最短路 完全背包)

    题意: 给定6个硬币的币值, 问组成1~100这些数最少要几个硬币, 比如给定1 2 5 10 20 50, 组成40 可以是 20 + 20, 也可以是 50 -10, 最少硬币是2个. 分析: 这 ...

  10. 数据结构实验6:C++实现二叉树类

    实验6 学号:     姓名:      专业:   6.1 实验目的 掌握二叉树的动态链表存储结构及表示. 掌握二叉树的三种遍历算法(递归和非递归两类). 运用二叉树三种遍历的方法求解有关问题. 6 ...