ftp服务器实现c

时间:2014-07-19 21:01:11

标签: 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;
    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);

在第一次运行中我得到了好结果:

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

但经过几次重复我得到:     num是:8     full_file = ol / 123.c     firstOcc = /123.c     firstOcc = /123.c     len是:6     num = 8,len = 6,num-len = 2     dl dir是ol @     名称目录是123.c     内容目录是你好我的名字是ohad123456

目录变得混乱,我找不到原因

0 个答案:

没有答案