在Ubuntu套接字(操作不允许) [英] Sockets on Ubuntu (operation not permitted)

查看:378
本文介绍了在Ubuntu套接字(操作不允许)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我newbee和只是让linux下C ++中我的第一个步骤。
所以我对一些插座任务。我以下指南,尤其是此内容之一。和code例子不工作。我开始用这样的:

I'm newbee and just making my first steps in c++ under linux. So I have some task about sockets. I'm following guides, especially this one. And code examples are not working. I started with 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的)必须有一个更多包括:

I've figured out that to compile it (Code::Blocks) there must be one more include:

#include <unistd.h>

但是,成功运行后我得到的消息绑定:不允许操作。哪里不对?我试图在根下运行它,它仍然是行不通的。

But after successful run I'm getting message "Bind: Operation not permitted". What is wrong? I've tried to run it under root and still it is not working.

推荐答案


您可以
 的#define SOCK_PATH的/ home /用户名/ echo_socket
它会正常运行。

Because of no permission. You can #define SOCK_PATH "/home/username/echo_socket" and it will run normally.

这篇关于在Ubuntu套接字(操作不允许)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆