我是newbee,只是在linux下开始使用C++的第一步。 所以我有一些关于套接字的任务。我正在关注指南,尤其是this之一。而代码示例不起作用。我开始与此:Ubuntu上的套接字(不允许操作)
#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;
}
我已经想通了,编译它(代码::块),必须有一个更包括:
#include <unistd.h>
但成功运行后,我收到消息“绑定:不允许操作”。哪里不对?我试图在root下运行它,但仍然无法正常工作。
用sudo运行你的应用程序 – SjB
也许你遇到了这个问题? http://stackoverflow.com/questions/6826407/why-i-cant-create-raw-socket-in-ubuntu – alf
只需使用运行按钮。我想知道如何配置Code :: Blocks在sudo下运行它?它执行xterm。 – binariti