[英]AF_UNIX socket in Linux above Windows WSL fails to bind to /mnt file: error 95, Operation not supported
我們需要將一個 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 : "Operation not supported" 我試過用 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];
int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
assert(sfd != -1);
// Delete any file that already exists at the address. Make sure the deletion
// succeeds. If the error is just that the file/directory doesn't exist, it's fine.
ret = remove(filename);
assert(ret != -1 || errno == ENOENT);
// Zero out the address, and set family and path.
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
assert(strlen(filename) <= sizeof(addr.sun_path) - 1);
strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);
ret = bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (ret == -1) printf("errno : %d - %s\n", errno, strerror(errno));
assert(ret != -1);
ret = listen(sfd, 1);
assert(ret != -1);
printf("Waiting to accept a connection...\n");
// NOTE: blocks until a connection request arrives.
int cfd = accept(sfd, NULL, NULL);
assert(cfd != -1);
printf("Accepted socket fd = %d\n", cfd);
char cmd;
char res[32];
while (1)
{
// get char from Win side
ssize_t num_read = read(cfd, (void *) &cmd, sizeof(cmd));
assert(num_read == sizeof(cmd));
printf(" cmd=%c\n", cmd);
// generate reply
sprintf(res, "Hello from Linux, %c\n", cmd);
// send data
ssize_t num_written = write(cfd, (const void *) res, sizeof(res));
assert(num_written == sizeof(res));
}
(void) close(cfd);
(void) close(sfd);
printf("done.\n");
return 0;
}
巧合的是,我昨天在WSL Github 問題上搜索有關 X11 套接字的信息時遇到了這個(與我的主題無關,但與您的主題相關)關於 WSL2 中的 AF_UNIX 支持的問題,這引起了我的注意,僅僅是因為它最近作為By Design關閉了。
簡而言之,目前 WSL2 不支持 AF_UNIX。 如果您的應用程序可能,請考慮將 WSL 發行版轉換(或復制)到支持 AF_UNIX 的 WSL1。
或者,您可以使用 Windows 和 Linux 端之間的網絡連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.