java unix domain socket demo

java16及之后的版本支持了java unix domain socket特性
但是java16之前,如果需要使用java unix domain socket特性,可以使用如下方式

protected void init_unixsock(Ruby runtime, IRubyObject _path, boolean server) {
  ByteList path = _path.convertToString().getByteList();
  String fpath = Helpers.decodeByteList(runtime, path);
  int maxSize = 103; // Max size from Darwin, lowest common value we know of
  if (fpath.length() > 103) {
    throw runtime.newArgumentError("too long unix socket path (max: " + maxSize + "bytes)");
  }
  try {
    if (server) {
      UnixServerSocketChannel channel = UnixServerSocketChannel.open();
      UnixServerSocket socket = channel.socket();
      // TODO: listen backlog
      socket.bind(new UnixSocketAddress(new File(fpath)));
      init_sock(runtime, channel, fpath);
    } else {
      File fpathFile = new File(fpath);
      if (!fpathFile.exists()) {
        throw runtime.newErrnoENOENTError("unix socket");
      }
      UnixSocketChannel channel = UnixSocketChannel.open();
      channel.connect(new UnixSocketAddress(fpathFile));
      init_sock(runtime, channel);
    }
  } catch (IOException ioe) {
    throw runtime.newIOErrorFromException(ioe);
  }
}

需要添加三方库依赖

<dependency>
    <groupId>com.github.jnr</groupId>
    <artifactId>jnr-unixsocket</artifactId>
    <version>0.38.8</version>
</dependency>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值