代码之家  ›  专栏  ›  技术社区  ›  DHamrick

在linux中测试套接字是否打开

  •  1
  • DHamrick  · 技术社区  · 17 年前

    setup_socket();
    
    while(1)
    {
       listen();
       newfile_descriptor = accept();
       int command
       read(newfile_descriptor,&command,sizeof(int));
       switch(command)
       {
          ...
       }
    }
    

    6 回复  |  直到 17 年前
        1
  •  2
  •   caskey    17 年前

    你读命令的循环怎么样:

    setup_socket();
    
    while(1)
    {
       listen();
       newfile_descriptor = accept();
       int command
       command = read(newfile_descriptor,&command,sizeof(int));
       while(command) {
         switch(command)
         {
            ...
         }
         // get next command, or figure out closed connection
         command = read(newfile_descriptor,&command,sizeof(int));
      }
    }
    
        2
  •  2
  •   Nikolai Fetissov    17 年前

    select / poll

        3
  •  1
  •   Matt Price    17 年前

    检查一下你是否能从插座中读取更多信息,怎么样?我认为,如果没有更多信息,你应该在命令结束时关闭连接。

        4
  •  1
  •   IRBMe    17 年前

    setup_socket();
    
    while(1) {
        listen();
        newfile_descriptor = accept();
    
        int command;
        do {
            command = read(newfile_descriptor,&command,sizeof(int));
    
            if (command > 0) {
                switch(command) {
                    ...
                }
           }
        } while (command > 0);
    }
    
        5
  •  1
  •   Matt Ball    17 年前

    要详细说明尼古拉的回应,请查看不可或缺的 Beej's guides .

    Here 是select()的具体部分,我知道,但以前从未实际使用过。

        6
  •  1
  •   foobar    15 年前

    第一件事。.. 将listen()移出while循环。

    推荐文章