fread()在我的FTP客户端实现中导致段错误

时间:2015-04-27 13:22:28

标签: c ftp

感谢您阅读此内容。我已经被困在这个bug上大约5个小时了,我只能想象它是让我继续前进的最微小的事情。我正试图从头开始编写FTP服务器/客户端。客户端发送“get / send(filename)”,服务器根据clients命令发送或接收它。我陷入了一个很早的阶段,现在我只是想让它打开文件,读取它或尽可能多的它到缓冲区,然后打印它。一旦我知道它打印正确,我就会将该缓冲区发送给我的客户端。我被困在那里,据我所知,它正确打开,因为它不会产生空指针。然而,在打印文件大小并成功将其发送到客户端后,它总是会出现段错误(我已尝试打开和读取文件描述符以及fopen和fread on FILE *,我对这两者都进行了分类,这就是为什么我认为这是其他地方的一个小错误。)所有的帮助表示赞赏,这是我的服务器代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <fcntl.h>

int main()
{

  char str[100];
  int listen_fd, comm_fd, rc;

  struct sockaddr_in servaddr;

  listen_fd = socket(AF_INET, SOCK_STREAM, 0);

  bzero( &servaddr, sizeof(servaddr));

  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htons(INADDR_ANY);
  servaddr.sin_port = htons(60072);

  bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));

  listen(listen_fd, 10);

  comm_fd = accept(listen_fd, (struct sockaddr*) NULL, NULL);
  while(1) {
    int bytes_read = 0, bytes_sent = 0;
    int which = 0, i = 0, filesize = 0, fd;
    size_t length;
    int offset;
    char *in[15], *token, filename[50];
    struct stat stat_buf;
    bzero( str, 100);

    // Read client command                                                                                                                                                                                                         
    read(comm_fd,str,100);
    printf("Seerver is processing command - %s", str);

    // Parse client command and determine send/recv                                                                                                                                                                                
    token = strtok(str, " ");
    while (token != NULL) {
      in[i] = malloc(strlen(token) + 1);
      strncpy(in[i], token, strlen(token));
      i++;
      if(strcmp(token, "get") == 0) which = 1;
      if(strcmp(token, "send") == 0) which = 2;
      if(strcmp(token, "exit") == 0) exit(0);
      token = strtok(NULL, " ");
      if(i == 1) {
        strcpy(filename, token);
      }
    }
    // Strip newline char sent with fgets                                                                                                                                                                                          
    size_t ln = strlen(filename) - 1;
    if (filename[ln] == '\n') {
      filename[ln] = '\0';
    }
    // Code for SENDING file                                                                                                                                                                                                       
    if (which == 1) {
      char buffer[100];
      // Open file and send file size to client                                                                                                                                                                                    
      stat(filename, &stat_buf);
      filesize = stat_buf.st_size;
      printf("%s successfully opened and has size of %d\n", filename, filesize);
      send(comm_fd, &filesize, sizeof(filesize), 0);
      FILE *fpt;
      bzero(buffer, 100);
      fopen(filename, "r");
      fread(buffer, sizeof(char), 99, fpt);
      printf("%s", buffer);
    }
   }
  }

这是我的客户代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>


int main(int argc,char **argv)
{
  int sockfd, n;
  char sendline[100];
  char recvline[100];
  char filedesc[100];
  struct sockaddr_in servaddr;

  sockfd=socket(AF_INET,SOCK_STREAM,0);
  bzero(&servaddr,sizeof servaddr);

  servaddr.sin_family=AF_INET;
  servaddr.sin_port=htons(60072);

  inet_pton(AF_INET,"127.0.0.1",&(servaddr.sin_addr));

  connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

  while(1)
    {
      int filesize = 0, fd, read_bytes = 0;
      char buffer[1024];
      bzero(sendline, 100);
      bzero(recvline, 100);
      bzero(filedesc, 100);
      printf("Send: ");
      fgets(sendline,100,stdin);
      // Send filename and command to server                                                                     
      write(sockfd,sendline,strlen(sendline)+1);
      // Receive file size from server                                                                           
      recv(sockfd, &filesize, sizeof(filesize), 0);
      printf("The file is %d bytes\n", filesize);
      fd = open("copy.txt", O_CREAT | O_WRONLY, 0644);
      if (fd < 0) {
        printf("Error creating copy of file");
        exit(0);
      }
      //recv(sockfd, &buffer, filesize, 0);                                                                      
      //printf("%s", buffer);                                                                                    
    }
}

我根本不会在这里发布任何内容,所以如果我能为你们提供任何其他信息或者将来会做些不同的事情,我会全力以赴。

再次感谢。

0 个答案:

没有答案