Ubuntu上的套接字(不允许操作)

我是新手,只是在Linux下我的第一步.
所以我有一些关于套接字的任务.我正在关注指南,尤其是 this指南.代码示例不起作用.我从这开始:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#define SOCK_PATH "echo_socket"

int main(void)
{
    int s,s2,t,len;
    struct sockaddr_un local,remote;
    char str[100];

    if ((s = socket(AF_UNIX,SOCK_STREAM,0)) == -1) {
        perror("socket");
        exit(1);
    }

    local.sun_family = AF_UNIX;
    strcpy(local.sun_path,SOCK_PATH);
    unlink(local.sun_path);
    len = strlen(local.sun_path) + sizeof(local.sun_family);
    if (bind(s,(struct sockaddr *)&local,len) == -1) {
        perror("bind");
        exit(1);
    }
return 0;
}

我已经想出要编译它(Code :: Blocks)必须还有一个包含:

#include <unistd.h>

但成功运行后,我收到消息“Bind:Operation not permitted”.哪里不对?我试图在root下运行它仍然无法正常工作.

有些Unix系统不允许你到处创建套接字.确保您拥有正确的权限和正确的文件系统. (fat32因为在手机上的sdcards上使用它不会允许额外的文件标记,可能会让你陷入麻烦)
最后在较新的系统上运行像selinux这样的安全事件可能会阻止套接字的创建.

在我的例子中,我不得不改变

#define SOCK_PATH "echo_socket"

#define SOCK_PATH "/dev/socket/echo_socket"

之后它立即起作用. (可执行文件在root shell中启动)

相关文章

目录前言一、创建Hadoop用户二、更新apt和安装Vim编辑器三、...
原文连接:https://www.cnblogs.com/yasmi/p/5192694.html ...
电脑重启后,打开VirtualBox,发现一直用的虚拟机莫名的消失...
参见:https://blog.csdn.net/weixin_38883338/article/deta...
Ubuntu 18.04 LTS 已切换到 Netplan 来配置网络接口。Netpla...
介绍每个 Web 服务都可以通过特定的 URL 在 Internet 上访问...