Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.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
为什么这个服务器不输出任何东西? #包括 #包括 #包括 #包括 #包括 #包括 int main() { 内特袜子,纽斯托克; struct sockaddr server_name={AF_UNIX,“Fred”}; socklen_t len=sizeof(struct sockaddr)+5; if((sock=socket(AF\u UNIX,sock\u STREAM,0))=-1) { printf(“创建套接字时出错”); 返回-1; } if(bind(sock和server_name,len)!=0)//绑定 { printf(“套接字绑定错误”); 返回-1; } if(listen(sock,10)!=0)//将sock设置为listen { printf(“错误监听”); 返回-1; } printf(“等待连接…”); 而(1) { newsock=accept(sock,(struct sockaddr*)NULL,NULL); 字符温度[1000]=“gggggg\n”; 写入(newsock、temp、strlen(temp)); 写入(newsock、temp、strlen(temp)); 关闭(新闻组); 睡眠(1); } 返回0; }_C_Loops_Printf - Fatal编程技术网

为什么这个服务器不输出任何东西? #包括 #包括 #包括 #包括 #包括 #包括 int main() { 内特袜子,纽斯托克; struct sockaddr server_name={AF_UNIX,“Fred”}; socklen_t len=sizeof(struct sockaddr)+5; if((sock=socket(AF\u UNIX,sock\u STREAM,0))=-1) { printf(“创建套接字时出错”); 返回-1; } if(bind(sock和server_name,len)!=0)//绑定 { printf(“套接字绑定错误”); 返回-1; } if(listen(sock,10)!=0)//将sock设置为listen { printf(“错误监听”); 返回-1; } printf(“等待连接…”); 而(1) { newsock=accept(sock,(struct sockaddr*)NULL,NULL); 字符温度[1000]=“gggggg\n”; 写入(newsock、temp、strlen(temp)); 写入(newsock、temp、strlen(temp)); 关闭(新闻组); 睡眠(1); } 返回0; }

为什么这个服务器不输出任何东西? #包括 #包括 #包括 #包括 #包括 #包括 int main() { 内特袜子,纽斯托克; struct sockaddr server_name={AF_UNIX,“Fred”}; socklen_t len=sizeof(struct sockaddr)+5; if((sock=socket(AF\u UNIX,sock\u STREAM,0))=-1) { printf(“创建套接字时出错”); 返回-1; } if(bind(sock和server_name,len)!=0)//绑定 { printf(“套接字绑定错误”); 返回-1; } if(listen(sock,10)!=0)//将sock设置为listen { printf(“错误监听”); 返回-1; } printf(“等待连接…”); 而(1) { newsock=accept(sock,(struct sockaddr*)NULL,NULL); 字符温度[1000]=“gggggg\n”; 写入(newsock、temp、strlen(temp)); 写入(newsock、temp、strlen(temp)); 关闭(新闻组); 睡眠(1); } 返回0; },c,loops,printf,C,Loops,Printf,“等待连接…”不会出现在我的屏幕上。有什么问题吗?我到处都试过打印功能。什么也没出现,怎么了?我看不出我关闭了stdout.有人能帮忙吗?谢谢。默认情况下,标准输出是行缓冲的。您的程序输出一些没有新行的内容,然后进入循环。您需要添加一个新行,如下所示: #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/typ

“等待连接…”不会出现在我的屏幕上。有什么问题吗?我到处都试过打印功能。什么也没出现,怎么了?我看不出我关闭了stdout.有人能帮忙吗?谢谢。

默认情况下,标准输出是行缓冲的。您的程序输出一些没有新行的内容,然后进入循环。您需要添加一个新行,如下所示:

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

int main()
{

int sock, newsock;
    struct sockaddr server_name = {AF_UNIX, "Fred"};
    socklen_t len=sizeof(struct sockaddr)+5;

    if( (sock=socket(AF_UNIX,SOCK_STREAM,0)) ==-1)
    {
        printf("error creating socket");
        return -1;
    }

    if( bind(sock,&server_name,len) != 0 ) //binding
    {

        printf("socket bind error ");
        return -1;
    }


   if(listen(sock,10)!=0)  //set sock to listen
   {
        printf("error listening");
        return -1;
   }

    printf("Waiting for connections....");

   while(1)
   {
        newsock=accept(sock, (struct sockaddr*)NULL, NULL);

        char temp[1000]="gggggg\n";
        write(newsock,temp,strlen(temp));

        write(newsock,temp,strlen(temp));
        close(newsock);

        sleep(1);

   }

return 0;
}
出于类似的原因,错误消息应该输出为标准错误,因为它没有缓冲

printf("Waiting for connections....\n");

首先,您应该添加“\n”并在日志末尾添加“等待连接…”\n,这样日志就不能被缓冲并被输出

其次,您的程序出错,正确的程序如下:

fprintf(stderr, "error creating socket\n");
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
内特袜子,纽斯托克;
结构sockaddr\u un server\u name;
socklen\u t len=sizeof(服务器名称);
取消链接(“Fred”);
if((sock=socket(AF\u UNIX,sock\u STREAM,0))=-1)
{
printf(“创建套接字时出错\n”);
返回-1;
}
server\u name.sun\u family=AF\u UNIX;
strcpy(server_name.sun_path,“Fred”);
if(bind(sock,(struct sockaddr*)&服务器名称,len)!=0)//绑定
{
printf(“套接字绑定错误\n”);
返回-1;
}
if(listen(sock,10)!=0)//将sock设置为listen
{
printf(“错误侦听\n”);
返回-1;
}
printf(“等待连接…”\n);
而(1)
{
newsock=accept(sock,(struct sockaddr*)NULL,NULL);
字符温度[1000]=“gggggg\n”;
写入(newsock、temp、strlen(temp));
写入(newsock、temp、strlen(temp));
关闭(新闻组);
睡眠(1);
}
返回0;
}

输出被缓冲,直到您打印换行符或调用
fflush(stdout)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

int main()
{

    int sock, newsock;
    struct sockaddr_un server_name;
    socklen_t len= sizeof(server_name);
    unlink("Fred");
    if( (sock=socket(AF_UNIX,SOCK_STREAM,0)) ==-1)
    {
        printf("error creating socket\n");
        return -1;
    }

    server_name.sun_family = AF_UNIX;
    strcpy(server_name.sun_path, "Fred");

    if( bind(sock,(struct sockaddr*)&server_name,len) != 0 ) //binding
    {

        printf("socket bind error \n");
        return -1;
    }


   if(listen(sock,10)!=0)  //set sock to listen
   {
        printf("error listening\n");
        return -1;
   }

   printf("Waiting for connections....\n");

   while(1)
   {
        newsock=accept(sock, (struct sockaddr*)NULL, NULL);

        char temp[1000]="gggggg\n";
        write(newsock,temp,strlen(temp));

        write(newsock,temp,strlen(temp));
        close(newsock);

        sleep(1);

   }

return 0;
}