在另外一个客户端执行 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. python爬虫---从零开始(二)Urllib库

    接上文再继续我们的爬虫,这次我们来述说Urllib库 1,什么是Urllib库 Urllib库是python内置的HTTP请求库 urllib.request 请求模块 urllib.error 异常 ...

  2. PHP06 流程控制

    学习要点 选择结构 循环结构 学习目标 掌握PHP的选择结构 掌握PHP的循环结构 流程控制概述 程序 程序:一系列计算机指令的集合. 编程语言:开发程序的工具. 程序执行结构 计算机程序有三种基本执 ...

  3. Linux-04 Linux中Tomcat和MySQL的安装

    1.下载apache-tomcat-7.0.79-tar.tar2.解压到当前用户目录,改名为tomcat [hduser@node1 ~]$ tar -zxvf apache-tomcat-7.0. ...

  4. 关于Java多线程(JAVA多线程实现的四种方式)

    Java多线程实现方式主要有四种:继承Thread类.实现Runnable接口.实现Callable接口通过FutureTask包装器来创建Thread线程.使用ExecutorService.Cal ...

  5. SqlSugar直接执行Sql

    参考:http://www.codeisbug.com/Doc/8/1132 我的思路: 1.数据库中写好sql 2.用SqlSugar直接执行sql,获取DataTable的数据 3.DataTab ...

  6. RHEL6.5 DHCP服务器搭建

    RHEL6.5 DHCP服务器搭建: DHCP服务器是用来分配给其它客户端IP地址用的,在RHEL 6.5中DHCP服务器搭建方法如下: 第一步,通过yum安装dhcp服务: 命令:yum insta ...

  7. python3.x Day6 协程

    协程:#定义来自牛人alex博客协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程.协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈 ...

  8. 洛谷 P1938 [USACO09NOV] 找工就业Job Hunt

    这道题可以说是一个复活SPFA的题 因为数据比较小,SPFA也比较简单 那就复习(复读)一次SPFA吧 #include<iostream> #include<cstdio> ...

  9. zoj 2736 Daffodil number

    Daffodil number Time Limit: 2 Seconds      Memory Limit: 65536 KB The daffodil number is one of the ...

  10. 在C#代码中应用Log4Net系列教程(附源代码)地址

    在博客园看到一篇关于Log4Net使用教程,比较详细,感谢这位热心的博主 博客园地址:http://www.cnblogs.com/kissazi2/archive/2013/10/29/339359 ...