c - Readdir() 错误输出

标签 c linux

使用server and client代码。 我尝试添加通过 readdir() 实现的 ls 功能

我的 *connection_handler 看起来像这样:

void *connection_handler(void *socket_desc)
{
  //Get the socket descriptor
  int sock = *(int*)socket_desc;
  int n;
  int user_exists = 0;  

  char    sendBuff[3000], client_message[2000];
  char *command;
  char home_dir[500] = "simple_slash/simple_home/";
  char pwd[500];
  DIR *direc;
  struct dirent *temp_name;

  while((n=recv(sock,client_message,2000,0))>0) {
      //send(sock,client_message,n,0);
    if (user_exists!=0) {   
    printf("inside if case\n");
    /* get the first token */
    command = strtok(client_message," ");
    printf("%s\n",command );
    if (strcmp(command,"ls")==0) {
    printf("inside ls\n");

    command = strtok(NULL, " ");
    //ls ki permissions ka check
    //strcat(pwd,command);
    printf("%s\n", pwd);
    if ((direc= opendir (pwd)) != NULL) {
      /* print all the files and directories within directory */
      strcpy(sendBuff,"\0");
      while ((temp_name = readdir (direc)) != NULL) {
        strcat(sendBuff,temp_name->d_name);
        strcat(sendBuff,"\n");
      //      printf ("%s\n", temp_name->d_name);
      }
      send(sock, sendBuff,sizeof(sendBuff),0);
      closedir (direc);
    } else {
      perror ("could not open directory ");
      //return EXIT_FAILURE;
    }
  }
  else if (strcmp(command,"fput")==0) {
    printf("inside fput\n");
  }
  else if (strcmp(command,"fget")==0) {

  }
  else if (strcmp(command,"create_dir")==0) {
    /* code */
  }
  else {
    n=0;
    send(sock, "Invalid command. Please try again.",n,0);
  }
}
else {
  user_exists = 1;
  strcat(client_message,"\0");
  strcat(pwd,home_dir);
  strcat(pwd,client_message);
  mkdir (pwd, 0777);
  strcpy(sendBuff,"Welcome, ");
  strcat(sendBuff,client_message);
  strcat(sendBuff,"\n");
  send(sock, sendBuff,sizeof(sendBuff),0); 
}
memset(client_message, 0, 2000);
}
close(sock);

  if(n==0)
  {
    puts("Client Disconnected");
  }
  else
  {
    perror("recv failed");
  }
return 0;
}

当我在客户端运行 ls 命令时,代码在服务器端完美运行(通过中间的 printf 语句进行验证)

但是输出会每三次返回到客户端。

服务器端终端输出:

abc@ubuntu:~/SE/hw1$ ./server

Socket created

bind done

Waiting for incoming connections...

Connection accepted

Handler assigned

inside if case

ls

inside ls

simple_slash/simple_home/user2

inside if case

ls

inside ls

simple_slash/simple_home/user2

inside if case

ls

inside ls

simple_slash/simple_home/user2

inside if case

ls

inside ls

simple_slash/simple_home/user2

inside if case

ls

inside ls

simple_slash/simple_home/user2

客户端终端输出:

abc@ubuntu:~/SE/hw1$ ./client 

Connected successfully.

Enter Username : user2

Welcome, user2

ls

ls

..

folder1

.

ls

ls

ls

..

folder1

.

第 1 行:欢迎<用户名>

第 2 行:ls

第 3 行:ls 给出输出

<小时/>

第 1 行:ls

第 2 行:ls

第 3 行:ls 给出输出

<小时/>

我在这里做错了什么?

最佳答案

What am i doing wrong here ?

问题开头提到的客户端代码仅在每次输入一行时从服务器连接读取一定量的数据 - 它不一定在到达时读取所有数据。为了使这种简单的方法发挥作用,服务器发送的字节数不得超过客户端愿意立即接收的字节数;由于您说每三次在客户端返回输出,服务器发送的字节数似乎是客户端期望接收的字节数的三倍,因此需要三次通过客户端循环才能完成仅从服务器收集一个响应。

我之所以写“work halfway”,是因为即使客户端的缓冲区足够大,也不能保证客户端通过一次 recv() 调用就能收到响应;您至少应该更改您的客户端,以便它循环直到从服务器读取完整的 sizeof sendBuff

关于c - Readdir() 错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28124586/

相关文章:

c - 如何编写设备的头文件

c - envz_get 和 getenv 是否引用相同的环境变量池?

Linux,将文件内容与文本字符串进行比较

c - 在 Fedora 16 中找不到 glib.h

linux - 查找登录源代码

c - core C 中什么效率更高? switch/if 语句?

c - 最大线程和最佳线程

c - 在 Arduino 中读取多个字符串

linux - Tiny Core linux : terminals database is inaccessible 上的 MariaDB

c++ - OpenGL函数需要调用两次才能生效