简体   繁体   English

直接从/dev/log读取数据 Unix Domain Socket

[英]Read data directly from /dev/log Unix Domain Socket

My project aims at reading log messages directly from /dev/log UNIX domain socket in Java. Currently I am using junixsocket .我的项目旨在直接从 Java 中的/dev/log UNIX 域套接字读取日志消息。目前我正在使用junixsocket Below is a sample code of client that reads from a unix socket.下面是从 unix 套接字读取的客户端示例代码。

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;
import org.newsclub.net.unix.AFUNIXSocketException;

public class SimpleTestClient {
    public static void main(String[] args) throws IOException {
        final File socketFile = new File("/dev/log");

        AFUNIXSocket sock = AFUNIXSocket.newInstance();
        try {
            sock.connect(new AFUNIXSocketAddress(socketFile));
        } catch (AFUNIXSocketException e) {
            System.out.println("Cannot connect to server. Have you started it?\n");
            System.out.flush();
            throw e;
        }
        System.out.println("Connected");

        InputStream is = sock.getInputStream();

        byte[] buf = new byte[8192];

        int read = is.read(buf);
        System.out.println("Server says: " + new String(buf, 0, read));
    
        is.close();

        sock.close();

        System.out.println("End of communication.");
    }
}

The above mentioned code is unable to connect to /dev/log .上面提到的代码无法连接到/dev/log It throws an exception:它抛出一个异常:

Cannot connect to server. Have you started it?
Exception in thread "main" org.newsclub.net.unix.AFUNIXSocketException: Protocol wrong type for socket (socket: /dev/log)
        at org.newsclub.net.unix.NativeUnixSocket.connect(Native Method)
        at org.newsclub.net.unix.AFUNIXSocketImpl.connect(AFUNIXSocketImpl.java:125)
        at org.newsclub.net.unix.AFUNIXSocket.connect(AFUNIXSocket.java:97)
        at org.newsclub.net.unix.AFUNIXSocket.connect(AFUNIXSocket.java:87)
        at SimpleTestClient.main(SimpleTestClient.java:40)

I am unable to figure out how to solve this problem.我无法弄清楚如何解决这个问题。 Any help would be appreciable.任何帮助将不胜感激。

Since you cannot connect to an existing server socket as mentioned in the log traces, then you haven't bound one one the mentioned file, so try creating an AF_UNIX server socket then connect to it. 由于无法按照日志跟踪中所述连接到现有服务器套接字,因此您尚未将提到的文件绑定到一个服务器上,因此请尝试创建一个AF_UNIX服务器套接字然后再连接到该服务器套接字。

It could be done in a separate class: 可以在单独的类中完成:

public class DevLogServer {

  public static void main(String[] args) throws IOException {

    final File socketFile = new File("/dev/log");
    AFUNIXServerSocket server = AFUNIXServerSocket.newInstance();
    try {
      server.bind(new AFUNIXSocketAddress(socketFile));
    } catch (Exception e) {
      throw e;
    }

  }

}

Edit as per @Ankit comment: 根据@Ankit注释进行编辑:

You may also need to make sure the syslod daemon is stopped by runnig below command in a terminal window: 您可能还需要确保在终端窗口中通过以下命令运行syslod来停止syslod守护程序:

sudo service syslog stop

You may alternatively need to grand write permission to the /dev directory. 或者,您可能需要获得对/ dev目录的大写权限。

您是否以root特权启动应用程序?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM