ftp服务器实现c

ftp服务器实现c,c,ftp,C,Ftp,我在使用ftp服务器时遇到问题,这是连接后我的主要功能: printf("Got a connection file transfer\n"); int tmp=recv(newsock,buff,BUFLEN,0); printf ("got %d bytes - bufer size\n", tmp); printf("buff s %s \n",buff); printf("\n"); char *start=NULL; star

我在使用ftp服务器时遇到问题,这是连接后我的主要功能:

    printf("Got a connection file transfer\n");
    int tmp=recv(newsock,buff,BUFLEN,0);
    printf ("got %d bytes - bufer size\n", tmp);
    printf("buff s %s \n",buff);

    printf("\n");

    char *start=NULL;
    start= (char *) malloc (INTLEN);
    bzero (start,INTLEN);
    memcpy(start,buff,INTLEN);
    int num=ntohl(*(int *)&start[0]);
    printf ("num is : %d\n", num);
    free(start);

    char *full_file=NULL;
    full_file=(char *) malloc (num);
    bzero (full_file,num);
    memcpy(full_file,buff+INTLEN,num);
    printf("full_file = %s \n" , full_file);


    char *firstOcc,*tempc,*lastOcc;
    char *pattern="/";
    firstOcc=strstr(full_file,pattern);
    tempc=firstOcc;
    printf("firstOcc = %s \n" , firstOcc);

    do {
      lastOcc=tempc;
      tempc=strstr(lastOcc+1,pattern);
    } while (tempc !=0);

    int len=strlen(lastOcc);
            printf("firstOcc = %s \n" , lastOcc);

    printf ("len is : %d\n", len);
    char *dirct=NULL;
    printf (" num = %d , len = %d and num-len = %d \n",num,len,num-len);
    dirct= (char *) malloc (num-len);
    memset(dirct,0,num-len);

    memcpy(dirct,full_file,num-len);
    printf("dl dir is %s \n",dirct);
    char *name=NULL;
    name= (char *) malloc (len-1);
    bzero (name,len-1);
    memcpy(name,full_file+num-len+1,len-1);
            printf("name dir is %s \n",name);
    char *content=NULL;
    content= (char *) malloc (tmp-8-num);
    bzero (content,tmp-8-num);
    memcpy(content,buff+8+num,tmp-8-num);
    printf("content dir is %s \n",content);

    FILE* pFile;
    struct stat st = {0};
    if (stat(dirct,&st)==-1){
      printf("need to make dir\n");
      mkdir (dirct,0777);
    }
    pFile = fopen(full_file,"wb");

    if (pFile){
      fwrite(content,tmp-8-num,1,pFile);
      }

      while (!(tmp<BUFLEN)){
        printf("big file\n");
      tmp=recv(newsock,buff,BUFLEN,0);
      fwrite(buff,tmp,1,pFile);
      }

    fclose(pFile);
    free(full_file);
    free(name);
    free(dirct);
    free(content);
    free(buff);
    free(buffer);
    firstOcc="";
    tempc="";
    lastOcc="";
  }
  close(sock);
  close(newsock);
  pthread_exit(0);
但经过几次重复,我得到: 数字是:8 完整文件=ol/123.c firstOcc=/123.c firstOcc=/123.c 莱恩:6岁 num=8、len=6和num len=2 dl-dir就是ol�@ 名字是123.c 内容目录是你好我的名字是ohad123456


目录变得凌乱,我找不到为什么:int tmp=recvnewsock,buff,bufflen,0;已获取,需要为\0的每个malloc添加+1
num is : 8
full_file = ol/123.c 
firstOcc = /123.c 
firstOcc = /123.c 
len is : 6
num = 8 , len = 6 and num-len = 2 
dl dir is ol 
name dir is 123.c 
content dir is Hello my name is ohad123456 
need to make dir