423,350 Members | 2,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,350 IT Pros & Developers. It's quick & easy.

ftp command socket is blocked after uploading a file?

P: 1
Expand|Select|Wrap|Line Numbers
  1. // non-block
  2. set_flag(client_data_socket, O_NONBLOCK);
  3. pid_t pid;
  4. if ((pid = fork()) < 0) {
  5.     printf("fork error");
  6.     continue;
  7. } else if (pid == 0) {
  8.     FILE *fp;
  9.     if ((fp = fopen(filename, "rb")) == NULL)
  10.     {
  11.         close(client_data_socket);
  12.         printf("open file failed\n");
  13.         exit(1);
  14.     }
  15.     size_t char_size = sizeof(char);
  16.     char data_buffer[FILE_READ_BUFFER_SIZE];
  17.     int numread;
  18.     for (;;)
  19.     {
  20.         bzero(data_buffer, FILE_READ_BUFFER_SIZE);
  21.         numread = fread(data_buffer, char_size, FILE_READ_BUFFER_SIZE, fp);
  22.         if (numread < 0)
  23.         {
  24.             printf("read file failed\n");
  25.             break;
  26.         } 
  27.         else if (numread > 0)
  28.         {
  29.             int length = send(client_data_socket, data_buffer, numread, 0);
  30.             if (length == 0)
  31.             {
  32.                 break;
  33.             }
  34.             else if (length < 0)
  35.             {
  36.                 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
  37.                 {
  38.                     continue;
  39.                 }
  40.                 printf("[PUT] command send data failed\n");
  41.                 exit(1);
  42.             }
  43.         }
  44.         if (numread == FILE_READ_BUFFER_SIZE) continue;
  45.         else {
  46.             break;
  47.         }
  48.     }
  49.     close(client_data_socket);
  50.     fclose(fp);
  51.     exit(0);
  52. } else {
  53.     int status = 0;
  54.     waitpid(pid, &status, 0);
  55.     if (status == 0)
  56.         printf("send file %s complete.\n", filename);
  57.     else 
  58.         printf("send file %s failed.\n", filename);
  59. }
  60.  
I closed the data socket after STOR a file, however, I found the cmd socket is blocked.
Attached Images
File Type: jpg 3433459856-5b29f7b018f36_articlex.jpg (72.3 KB, 14 views)
Jun 21 '18 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.