简体   繁体   English

Readdir()错误输出

[英]Readdir() erroneous output

Using the server and client code. 使用服务器和客户端代码。 I tried adding ls functionality implemented via the readdir() 我尝试添加通过readdir()实现的ls功能

My *connection_handler looks like this: 我的* 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;
}

When i run the ls command on the client side the code runs perfectly on the server side (verified by the printf statements in between) 当我在客户端运行ls命令时,代码在服务器端完美运行(由之间的printf语句验证)

But the output is returned on the client side every third time. 但是输出每隔第三次在客户端返回一次。

Server side terminal ouput: 服务器端终端输出:

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

Client Side terminal output: 客户端终端输出:

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

Connected successfully.

Enter Username : user2

Welcome, user2

ls

ls

..

folder1

.

ls

ls

ls

..

folder1

.

Line 1: Welcome < username > 第1行:欢迎使用<用户名>

Line 2: ls 第2行:ls

Line 3: ls Gives output 第3行:ls提供输出


Line 1: ls 第1行:ls

Line 2: ls 第2行:ls

Line 3: ls Gives output 第3行:ls提供输出


What am i doing wrong here ? 我在这里做错了什么?

What am i doing wrong here ? 我在这里做错了什么?

The client code referred to at the beginning of the question reads a certain amount of data from the server connection only once each time a line is typed in - it does not necessarily read all data at the time of arrival. 问题开头提到的客户端代码每次键入一次行时都仅从服务器连接中读取一定数量的数据-不一定在到达时读取所有数据。 For this simplistic approach to work halfway it would be required that the server does not send more bytes than the client is willing to receive at once; 为了使这种简单的方法能够半途工作,将要求服务器发送的字节数不要超过客户端一次愿意接收的字节数。 since you say the output is returned on the client side every third time , the server seems to send about three times as many bytes as the client expects to receive, so it takes three passes through the client loop to collect just one response from the server. 由于您说输出是每隔第三次在客户端返回一次 ,因此服务器似乎发送的数据量大约是客户端希望接收的字节的三倍,因此需要经过三遍客户端循环才能从服务器收集一个响应。

I wrote "work halfway" because it is not guaranteed that the client receives a response with one recv() call even if its buffer is big enough; 我之所以写“中途工作”,是因为即使缓冲区足够大,也不能保证客户端通过一个recv()调用收到响应。 you should at least change your client so that it loops until it has read the full sizeof sendBuff from the server. 您至少应该更改您的客户端,以使其循环直到从服务器读取完整sizeof sendBuff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM