嘿,你能告诉我 unix 中的 sock_path 是什么吗?
我完全困惑了...我收到错误“非套接字上的套接字操作”。
此路径是否包含我们定义的结构以及存储该结构的位置! 我已经发布了我的整个客户端代码。
我在双方(客户端和服务器)上都遇到相同的错误...请帮助...我没有得到 sock_path 部分?
#define SOCK_PATH "echo_socket"
int main(void)
{
int s, t, len;
struct sockaddr_un remote;
char str[100];
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket"); exit(1);
}
printf("Trying to connect...\n");
remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCK_PATH);
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
int val=connect(s, (struct sockaddr *)&remote, len);
if ( val< 0) {
perror("connect");
exit(1);
}
printf("Connected.\n");
while(printf("> "), fgets(str, 100, stdin), !feof(stdin)) {
if (send(s, str, strlen(str), 0) == -1) {
perror("send");
exit(1);
}
if ((t=recv(s, str, 100, 0)) > 0) {
str[t] = '\0';
printf("echo> %s", str);
} else {
if (t < 0) perror("recv");
else printf("Server closed connection\n");
exit(1);
}
}
}
最佳答案
您是否尝试使用unix套接字进行文件操作?如果是,您分配给 sun_path 的 SOCK_PATH 应该是一个文件名。
看看这个.. http://beej.us/guide/bgipc/output/html/multipage/unixsock.html
关于c - 套接字操作上的非套接字错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11452600/