首页 > 解决方案 > C 编程通过套接字发送文件问题

问题描述

我正在尝试设置客户端和服务器程序,客户端可以从服务器下载文件。我已经让客户端下载了一个 .txt 文件,但是,.txt 文件只是被创建,其中没有任何数据被传输。

我发现错误来自服务器将文件大小发送到客户端或客户端无法识别 EOF 的位置。

这是我的服务器端代码:

int send_all(int clientSocket, const void *buffer, int len) {
  const char *pbuf = (const char *) buffer;

  while(len > 0) {
    int sent = send(clientSocket, pbuf, len, 0);
    printf("%d\n", sent);
    if(sent < 1) {
      printf("%s\n", "Can't write to socket");
      return -1;
    }
    pbuf += sent;
    len -= sent;
  }
  return(0);
}


void SendFileToClient(int clientSocket, char* fileName) {

  //int offset;
  char buffer[0x1000];
  char temp[512] = "/root/Documents/";
  const char* filename = fileName;
  struct stat s;

  strcat(temp, filename);
  printf("%s\n", temp);

  //append the filenameonto the directory


  if(stat(temp, &s) == -1) {
    printf("%s\n", "Can't get file info");
    return;
  }

  FILE *file = fopen(temp, "rb");
  if(!file) {
    printf("%s\n", "Can't open the file for reading");
    return;
  }

  off_t size = s.st_size;
  off_t tmp_size = ntohl(size);
  if(send_all(clientSocket, &tmp_size, sizeof(tmp_size)) == 0) {
    while(size > 0) {
      printf("%ld\n", size);
      int rval = fread(buffer, 1, min(sizeof(buffer), size), file);
      if(rval < 1) {
        printf("%s\n", "Cant read from file");
        break;
      }

      if (send_all(clientSocket, buffer, rval) == -1)
        break;
      size -= rval;
    }
  }
  fclose(file);
}

这是我的客户端代码:

int write_all(FILE *file, const void *buffer, int len) {
  const char *pbuf = (const char *) buffer;

  while (len > 0) {
    int written = fwrite(pbuf, 1, len, file);
    if(written < 1) {
      printf("%s\n", "Cant write to file");
      return -1;
    }
    pbuf += written;
    len -= written;
  }
  return 0;
}

int read_all(int clientSocket, void *buffer, int len) {
  char *pbuf = (char *) buffer;
  int total = 0;

  while (len > 0) {
    int rval = recv(clientSocket, pbuf, len, 0);
    if(rval < 0) {
      printf("%s\n", "Cant read from socket");
      return -1;
    }
    if(rval == 0) {
      printf("%s\n", "Socket disconnected");
      return 0;
    }

    pbuf += rval;
    len -= rval;
    total += rval;

  }
  return total;
}

void RecvFile(int clientSocket, const char* filename) {
  int rval;
  char buffer[0x1000];
  //printf("%s\n", filename);
  FILE *file = fopen(filename, "wb");
  if(!file) {
    printf("%s\n", "Cant open file for writing");
    return;
  }
  off_t size = 0;
  //this statement is not working
  if(read_all(clientSocket, &size, sizeof(size)) == 1) {
    size = ntohl(size);
    printf("%s%ld\n", "size: ", size);
    while(size > 0) {
      rval = read_all(clientSocket, buffer, min(sizeof(buffer), size));
      printf("%s%d\n", "rval: ", rval);
      if(rval < 1)
        break;
      if(write_all(file, buffer, rval) == -1) {
        printf("%s\n", "Cant write to file");
        break;
      }
    }
  }
  printf("%s\n", "closing file...");
  fclose(file);
}

如果代码看起来有点乱,我深表歉意——由于某种原因,它没有以良好的格式粘贴。

我相信这是问题的一部分,但即使通过各种调试,我似乎也无法让它工作:(

//this statement is not working
  if(read_all(clientSocket, &size, sizeof(size)) == 1) {

提前感谢您的帮助!

标签: csocketsnetworking

解决方案


推荐阅读