直接从/ dev / log Unix Domain Socket读取数据 [英] Read data directly from /dev/log Unix Domain Socket

查看:261
本文介绍了直接从/ dev / log Unix Domain Socket读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目旨在直接从Java中的 / dev / log UNIX域套接字读取日志消息。目前我正在使用 junixsocket 。下面是从unix套接字读取的客户端示例代码。

My project aims at reading log messages directly from /dev/log UNIX domain socket in Java. Currently I am using junixsocket. Below is a sample code of client that reads from a unix socket.

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.");
    }
}

上述代码无法连接到的/ dev /日志。它抛出异常:

The above mentioned code is unable to connect to /dev/log. It throws an exception:

无法连接到服务器。你有没有开始呢?
线程main中的异常org.newsclub.net.unix.AFUNIXSocketException:协议错误类型的socket(socket:/ dev / log)
at org.newsclub.net.unix.NativeUnixSocket.connect(本地方法)
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)
在SimpleTestClient.main(SimpleTestClient.java:40)

我无法弄清楚如何解决这个问题。任何帮助都会很明显。

I am unable to figure out how to solve this problem. Any help would be appreciable.

推荐答案

由于您无法连接到日志跟踪中提到的现有服务器套接字,不要将所提到的文件绑定一个,所以尝试创建 AF_UNIX 服务器套接字然后连接到它。

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.

可以单独完成class:

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;
    }

  }

}



< h2>根据@Ankit编辑评论:

您可能还需要确保 syslod 守护程序被停止终端窗口中的runnig below命令:

Edit as per @Ankit comment:

You may also need to make sure the syslod daemon is stopped by runnig below command in a terminal window:

sudo service syslog stop

您可能需要对 / dev 目录进行大写权限。

You may alternatively need to grand write permission to the /dev directory.

这篇关于直接从/ dev / log Unix Domain Socket读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆