Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 本地套接字中的绑定错误_C_Sockets_Unix_Bind_Unix Socket - Fatal编程技术网

C 本地套接字中的绑定错误

C 本地套接字中的绑定错误,c,sockets,unix,bind,unix-socket,C,Sockets,Unix,Bind,Unix Socket,我必须编写一些代码来完成以下任务: 客户端从键盘读取输入并将字符串发送到服务器。服务器打印客户端的IP和字符串。 编译时是可以的,但是当我运行代码时会出现绑定错误“没有这样的文件或目录” 我已经在论坛上查看了beej的指南和其他问题,但无法解决这个问题。 感谢您的帮助 编辑:我做了你让我做的更改,除了控制读写的返回值,因为程序没有进入while循环!另外,setsockopt不起作用 #include<unistd.h> #include<string.h> #inclu

我必须编写一些代码来完成以下任务: 客户端从键盘读取输入并将字符串发送到服务器。服务器打印客户端的IP和字符串。 编译时是可以的,但是当我运行代码时会出现绑定错误“没有这样的文件或目录” 我已经在论坛上查看了beej的指南和其他问题,但无法解决这个问题。 感谢您的帮助

编辑:我做了你让我做的更改,除了控制读写的返回值,因为程序没有进入while循环!另外,setsockopt不起作用

#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<sys/un.h>

int main(void)
{
int fd1,fd2,test,lun,n,enable=1;
socklen_t len;
struct sockaddr_un indirizzo_serv, indirizzo_cl;
char buf[100]={0},temp[100]={0};

fd1=socket(PF_LOCAL,SOCK_STREAM,0); //creo socket locale
if(fd1<0)
    perror("Errore nella creazione del socket\n");
else printf("Socket OK\n");

indirizzo_serv.sun_family=AF_LOCAL;
strcpy(indirizzo_serv.sun_path,"/tmp/socket1000");

//lun=strlen(indirizzo_serv.sun_path)+sizeof(indirizzo_serv.sun_family);

if(setsockopt(fd1,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int))<0)
    perror("Errore in setsockopt REUSEADDR\n");

test=bind(fd1,(struct sockaddr*)&indirizzo_serv,sizeof(indirizzo_serv));

if(test==0){
    printf("Bind OK\n");
    test=listen(fd1,2);
    if(test==0){
        printf("listen OK\n");
        len=sizeof(struct sockaddr_un);
        while(1){
            fd2=accept(fd1,(struct sockaddr*)&indirizzo_cl,&len);
            if(fd2<0)
                perror("\nErrore nella accept\n");
            else printf("Accept OK\n");

            write(STDOUT_FILENO,"Client - Inserire la stringa da inviare al server: \n",strlen("Client - Inserire la stringa da inviare al server: \n"));
            read(STDIN_FILENO,buf,sizeof(buf));
            write(fd2,buf,strlen(buf));//write(fd1,buf,strlen(buf));
            read(fd2,buf,strlen(buf));
            sprintf(temp,"Server - IP del client: %s\n",indirizzo_cl.sun_path);
            write(STDOUT_FILENO,temp,strlen(temp));
            memset(buf,0,100);
            memset(temp,0,100);
        }
    }
    else perror("Errore nella listen\n");
   }
   else perror("Errore nella bind\n");

   unlink(indirizzo_serv.sun_path);//close(fd1);
   return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
int fd1,fd2,测试,lun,n,enable=1;
索克伦;
结构sockaddr_un indirizzo_serv,indirizzo_cl;
char buf[100]={0},temp[100]={0};
fd1=socket(PF_LOCAL,SOCK_STREAM,0);//creo socket区域设置

如果(fd1编译仍然存在问题(
test=0
=>
test==0
int len;
=>
socklen\u t len;
(错误签名))。您应该使用
-Wall
编译,并在询问之前对其进行排序

您的错误很可能是由于路径中缺少目录。
替换确保存在的路径(如
/tmp/socket1
)可使其在我的电脑上工作。

如果要写入侦听套接字,请更改:



进入:


另外:您应该检查从
read()

以及:

总是错误的,因为这是一个赋值。(也许你想要
if(test==0){
?)

write(fd1,buf,strlen(buf));
-->
write(fd2,buf,strlen(buf));
仍然不工作test=0只是一个输入错误
write(fd1,buf,strlen(buf));
write(fd2,buf,strlen(buf));
if(test=0){