我是新手,只是在Linux下使用C ++迈出了第一步。所以我有一些关于套接字的任务。我正在遵循指南,尤其是这份指南。和代码示例不起作用。我从这个开始:
#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;
}
Run Code Online (Sandbox Code Playgroud)
我发现要编译它(代码:: Blocks),必须再包含一个:
#include <unistd.h>
Run Code Online (Sandbox Code Playgroud)
但是成功运行后,我会收到消息“绑定:不允许操作”。怎么了?我试图在根目录下运行它,但仍然无法正常工作。