使用Unix本地套接字编写下面的C源我得到了一个关于已经使用的地址的错误.在检查了man 7 Unix以获取更多信息之后,我尝试创建一个执行我的程序的子文件夹(显然修改了当前文件夹中的sun_path字段),但错误始终如一.
有人能帮助我吗?
源代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/un.h> #include <unistd.h> #include <errno.h> #define MAXLEN 128 int main (int argc,char *argv[]){ struct sockaddr_un server; int serverfd,clientfd; socklen_t addrsize = sizeof(struct sockaddr_un); char buff[MAXLEN],*path; if (argc < 2){ printf("Error: %s [MESSAGE]\n",argv[0]); return 1; } if ((serverfd = socket(AF_UNIX,SOCK_STREAM,0)) < 0){ printf("Error \"%s\" in socket()\n",strerror(errno)); exit(1); } puts("socket()"); server.sun_family = AF_UNIX; path = strcpy(server.sun_path,"/home/myhome/DropBox/Sources/C/sub"); printf("[DEBUG]Address bound at %s\n",path); if ((bind(serverfd,(struct sockaddr*)&server,addrsize)) < 0){ printf("Error \"%s\" in bind()\n",strerror(errno)); exit(1); } puts("bind()"); if ((listen(serverfd,1)) < 0){ printf("Error \"%s\" in listen()\n",strerror(errno)); exit(1); } if ((clientfd = accept(serverfd,NULL,&addrsize)) < 0){ printf("Error \"%s\" in accept()\n",strerror(errno)); exit(1); } write(clientfd,argv[1],strlen(argv[1])); read(clientfd,buff,sizeof(buff)); puts(buff); close(clientfd); close(serverfd); return 0; }