我的项目旨在直接从Java中的/dev/log
UNIX域套接字读取日志消息。目前我正在使用junixsocket。下面是从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.");
}
}
上述代码无法连接到/dev/log
。它引发了一个例外:
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)
我无法弄清楚如何解决这个问题。任何帮助都会很明显。
答案 0 :(得分:1)
由于您无法连接到日志跟踪中提到的现有服务器套接字,因此您尚未将所提到的文件绑定到一个,因此请尝试创建 AF_UNIX 服务器套接字,然后连接到它
可以在单独的课程中完成:
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;
}
}
}
您可能还需要确保终端窗口中的runnig below命令停止syslod
守护程序:
sudo service syslog stop
您可能需要对 / dev 目录进行大写权限。
答案 1 :(得分:0)
您是否以root权限启动应用程序?