标签: unix-socket

Windows WSL 上的 Linux 中的 AF_UNIX 套接字无法绑定到 /mnt 文件:错误 95,不支持操作

我们需要将 Windows 客户端应用程序连接到 Linux 服务器应用程序。Linux 端在 Windows 10 (10.0.19044) 中的 WSL2 之上运行。

我们想要使用 UNIX 域套接字,并遵循https://devblogs.microsoft.com/commandline/windowswsl-interop-with-af_unix/中的指导

服务器程序成功绑定到“本地”文件系统中的文件(例如/tmp/mysock),但无法绑定到已安装驱动器中的“Windows 端”文件(例如/mnt/c/mysock) ,这是服务器可以接受来自 Windows 端 AF_UNIX 套接字的连接所必需的。

我得到的 errno 是 95 :“不支持操作”我尝试使用 sudo 运行,但结果相同。

知道发生了什么事吗?

服务器代码是:

#include <sys/socket.h>
#include <sys/un.h>

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>

#undef NDEBUG

// filename comes as command-line argument
int main(int argc, char *argv[])
{
    struct sockaddr_un addr;
    int ret = -1;
    
    printf("Starting NVC_LINUX...\n");
    
    assert(argc == 2);
    char *filename = argv[1]; …
Run Code Online (Sandbox Code Playgroud)

linux bind unix-socket windows-subsystem-for-linux wsl-2

2
推荐指数
1
解决办法
2442
查看次数

在 C/Unix 中侦听 IP 子网的套接字

我正在尝试用 C 编写服务器-客户端套接字程序。目标是让服务器侦听特定端口,但跨越属于同一 IP 子网的 IP 地址范围。这个 IP 子网是127.xxx范围的一部分(当然不是 127.0.0.1)。

需要注意的几点:

  • 这是一个基于流的套接字,而不是数据报套接字。
  • 这不是广播地址。
  • 仅在 Unix/Linux 平台上用 C/C++ 实现

我不想为该范围内的每个 IP 地址在服务器上打开多个套接字。这是不可扩展的。

任何帮助将不胜感激。这甚至可行吗?

c c++ unix network-programming unix-socket

1
推荐指数
1
解决办法
2685
查看次数

更改linux套接字文件权限

首先,是的,这与这个堆栈溢出问题有关,但我的情况略有不同,我的帖子没有得到答案。

因此,在我的 Dell 桌面工作站 Ubuntu 10.04 32 位上,我开发了一个服务器程序,旨在为 Apache 运行的 PHP“程序”提供 Unix 域套接字。(注意:umask = 0022)我将套接字文件命名为/home/wmiller/ACT/web_socket_file。(ACT 是对产品名称的引用)。/home/wmiller/ACT 的权限为 777。 /home/wmiller/ACT/web_socket_file 的创建权限为 777。

现在,我将程序复制到我的测试平台,一个 Q7 格式的 Intel 处理器板,它也有 Ubuntu 10.04 32 位和 umask = 0022。相同的目录,相同的 777 目录权限。但是,现在当我运行代码 /home/wmiller/ACT/web_socket_file 时,会出现 755 权限,而 Apache/PHP 无法打开 Unix 域套接字,因为它获取的是 rx 权限,而不是 rw- 或 rwx。Apache 正在 uid = www-data 中运行。

sockaddr_un       webServAddr;
remove( g_webSocketFileName.c_str() );       // to erase any lingering file from last time

memset(&webServAddr, 0, sizeof(webServAddr));
webServAddr.sun_family        = AF_UNIX;
snprintf( webServAddr.sun_path, UNIX_PATH_MAX, "%s", g_webSocketFileName.c_str() ); …
Run Code Online (Sandbox Code Playgroud)

c++ sockets unix-socket

1
推荐指数
1
解决办法
5699
查看次数

为什么 select 调用不会在 unix 域套接字上阻塞?

我在谷歌上搜索了很多但没有得到答案,因此将其发布在这里。

在下面的 C 程序(服务器代码)中,我想要一个在/tmp/unix-test-socket. 我的问题是客户端代码能够成功连接到服务器。但是,一旦连接并且我“接受”了连接,select调用就不会阻塞。

所以让我解释一下。

最初的 unix_domain_socket = 3

一旦我收到第一个请求,就接受连接并将其存储在 unix_domain_socket_connections[max_unix_domain_socket_connections] 中。套接字的fd值为 4。

当我运行服务器代码时,它会进入一个循环,因为 select 调用认为套接字 4 中总是有数据。

我将客户端运行为:

./unix-client "/tmp/unix-test-socket" SEND_DATA
Run Code Online (Sandbox Code Playgroud)

SERVER端的输出:

Client sent us a message!

Successfully accepted the new ION connection with fd 4!

[program_select_to_look_at_right_sockets]: Storing fd 4

Data Arrived on UNIX domain socket 4

length 10 SEND_DATA  <-- I get the data sent by the client

[program_select_to_look_at_right_sockets]: Storing fd 4 *<-- Why isnt select blocking and why does it …
Run Code Online (Sandbox Code Playgroud)

c c++ sockets unix-socket

1
推荐指数
1
解决办法
3332
查看次数

监听unix套接字时检查发送者

我有一个监听 unix 套接字的进程。但是,在阅读之前,我想检查有关此传入消息的一些元数据,例如它的源进程(假设我想删除来自不可信发件人的消息)。是否有任何系统调用可以检索此信息。

    if(listen(sock_fd, 10) != 0) {
        assert("listen failed");
    }

    while((conn_fd = accept(sock_fd,
                       (struct sockaddr *) &address,
                       &address_length)) != -1) {
        int nbytes = 0;
        static char buffer[PAYLOAD_SZ];
        nbytes = (int)read(conn_fd, buffer, PAYLOAD_SZ);
Run Code Online (Sandbox Code Playgroud)

c sockets macos unix-socket xnu

1
推荐指数
1
解决办法
1357
查看次数

是否可以使用 Go 通过 Unix 域套接字发送和接收文件描述符?

unix(7)Unix 域套接字的手册页中,它说sendmsg可以与标志一起使用SCM_RIGHTS通过这些套接字发送文件描述符。Go 支持吗?有没有好的示例代码展示它是如何完成的?

包中似乎声明了一个Msghdr结构syscall。但没有函数接受它。也许我必须使用原始系统调用接口?

unix go unix-socket

1
推荐指数
1
解决办法
2557
查看次数

将Java gRPC服务器绑定到Unix域套接字

我有以下代码,它将创建一个Java gRPC服务器并尝试将其绑定到USD / tmp / foo:

  EpollEventLoopGroup group = new EpollEventLoopGroup();
  Server server =
      NettyServerBuilder.forAddress(new DomainSocketAddress("/tmp/foo"))
          .channelType(EpollServerSocketChannel.class)
          .bossEventLoopGroup(group)
          .workerEventLoopGroup(group)
          .addService(new Impl())
          .build()
          .start();
  server.awaitTermination();
Run Code Online (Sandbox Code Playgroud)

但是,此操作失败bind(..) failed: Invalid argument

Exception in thread "main" java.io.IOException: Failed to bind
        at io.grpc.netty.NettyServer.start(NettyServer.java:231)
        at io.grpc.internal.ServerImpl.start(ServerImpl.java:151)
        at io.grpc.internal.ServerImpl.start(ServerImpl.java:75)
        at com.google.devtools.javatools.jade.pkgloader.GrpcLocalServer.main(GrpcLocalServer.java:60)
Caused by: io.netty.channel.unix.Errors$NativeIoException: bind(..) failed: Invalid argument
        at io.netty.channel.unix.Errors.newIOException(Errors.java:117)
        at io.netty.channel.unix.Socket.bind(Socket.java:291)
        at io.netty.channel.epoll.AbstractEpollChannel.doBind(AbstractEpollChannel.java:714)
        at io.netty.channel.epoll.EpollServerSocketChannel.doBind(EpollServerSocketChannel.java:70)
        at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1283)
        at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501)
        at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486)
        at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:989)
        at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254)
        at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:364)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403)
        at …
Run Code Online (Sandbox Code Playgroud)

sockets bind unix-socket netty grpc-java

1
推荐指数
1
解决办法
1079
查看次数

如何通过本地套接字而不是在 Scala/java 中的 inet 创建 GRPC 服务

我的GRPC服务只能被本地机器上的应用程序访问。

我认为如果客户端通过 Unix 域套接字而不是 localhost:port 连接,它的执行速度会更快

我很想了解在这种情况下如何创建 grpc 服务,它应该能够在 CentOS 和 Mac 上运行

目前正在创建这样的服务器:

val server: Server = ServerBuilder.forPort(port).addService(GreeterGrpc.bindService(new GrpcServer, ec)).build().start()
Run Code Online (Sandbox Code Playgroud)

我也尝试过这样的配置:

val server: Server = io.grpc.netty.NettyServerBuilder.forPort(port).addService(ProtoReflectionService.newInstance()).addService(GreeterGrpc.bindService(new GrpcServer, ec)).build().start()
Run Code Online (Sandbox Code Playgroud)

但无法弄清楚如何绑定到本地套接字而不是本地主机

scala unix-socket netty grpc

1
推荐指数
1
解决办法
6385
查看次数

在 Dart 中使用 UNIX 套接字

目前我可以使用 TCP 套接字:

final socket = await Socket.connect('127.0.0.1', 8888);
Run Code Online (Sandbox Code Playgroud)

我想使用UNIX 套接字。有没有办法用 Dart 做到这一点?

unix-socket dart

1
推荐指数
1
解决办法
295
查看次数

关闭 Unix 套接字的正确方法

我正在使用从这个链接获得的代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>

#define DATA "Hello world"


int main()
{
  int sock;
  struct sockaddr_un server;

  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock < 0) {
      perror("opening stream socket");
      exit(1);
  }

  server.sun_family = AF_UNIX;
  strcpy(server.sun_path, "/tmp/foo.sock");

  int con = connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un));
  if (con < 0) {
      close(sock);
      perror("connecting stream socket");
      exit(1);
  }

  const char * D = "Hello world!";
  if (write(sock, DATA, sizeof(DATA)) < 0)
      perror("writing on stream …
Run Code Online (Sandbox Code Playgroud)

c unix-socket

1
推荐指数
1
解决办法
51
查看次数