首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一个关于socket,目录的有关问题,望大大们赐教

2012-07-29 
一个关于socket,目录的问题,望大大们赐教现在问题是这样的,假设我本地有很多目录,目录下面很多文件例如01

一个关于socket,目录的问题,望大大们赐教
现在问题是这样的,假设我本地有很多目录,目录下面很多文件例如01目录下面有201206222133.txt,201206222135.txt的文件,现在客服端向我们索要目录下面的文件列表,例如客服端发一个01目录下面的命定过来,我们就要把01下面的文件的文件名全部发给客服端,大概流程就是这样,我该怎么去实现呢??有情大大们了。

[解决办法]

C/C++ code
int sendDirList2( int clientfd, FILE *logFP ){    DIR *dir;    int sendBytes, recvBytes, fileCount, dirListFlag = 0;    struct dirent *dirp;    struct dirent **fileList;    _fileShortInfo fileShortInfo;    _fileAllInfo fileAllInfo;    char dirlistbuf[sizeof(fileShortInfo)];    char dirListName[NAMELEN];    char cwd[NAMELEN];    /*receive dir name*/    if ( (recvBytes = bufRecv(clientfd, dirListName, NAMELEN, 0)) == -1 )    {        perror("fail to receive dir name");        return -1;    }    getcwd(cwd, NAMELEN);    /*prevent client from getting upper dir list*/    if ( !strcmp(cwd, "/home/jimmy/FTP_FILES") && !strcmp(dirListName, "..") )    {        dirListFlag = -1;    }    memcpy(dirlistbuf, &dirListFlag, sizeof(dirListFlag));    /*send number of files in dir*/    if ( (sendBytes = send(clientfd, dirlistbuf, sizeof(dirListFlag), 0)) == -1 )    {        perror("fail to send dirlistFlag");        return -1;    }    /*prevent client from getting upper dir list*/    if( dirListFlag == -1 )    {        f2printf(stdout, logFP, "Illegal Operation!\n");    }    else    {        f2printf(stdout, logFP, "%s request dir \"%s\" list.Sending...",                inet_ntoa(clientSockAddr.sin_addr), dirListName);        memset(dirlistbuf, 0x00, sizeof(dirlistbuf));        /*open current dir on server*/        if ( (dir = opendir(dirListName)) == NULL )         {            perror("opendir");            return -1;        }        fileCount = scandir(dirListName, &fileList, 0, alphasort);        memcpy(dirlistbuf, &fileCount, sizeof(fileCount));        /*send number of files in dir*/        if ( (sendBytes = send(clientfd, dirlistbuf, sizeof(fileCount), 0)) == -1 )        {            perror("fail to send fileCount");            return -1;        }            chdir(dirListName);        /*read the dir and send file info to client*/        while ( (dirp = readdir(dir)) != NULL )        {            fileAllInfo.fileStat = getFileStat(dirp->d_name);            fileAllInfo.dirStat = *dirp;            memcpy(&fileShortInfo.fileName, &fileAllInfo.dirStat.d_name, sizeof(fileShortInfo.fileName));            fileShortInfo.flag = (int)fileAllInfo.dirStat.d_type;            fileShortInfo.fileSize = fileAllInfo.fileStat.st_size;                    memcpy(dirlistbuf, &fileShortInfo, sizeof(fileShortInfo));                /*send the file info everytime reads from "./" dir*/            if ( (sendBytes = send(clientfd, dirlistbuf, sizeof(fileShortInfo), 0)) == -1 )            {                perror("fail to send filestat");                return -1;            }            else if(sendBytes == 0)            {                break;            }        }    }    f2printf(stdout, logFP, "Down!\n");    closedir(dir);    return 0;} 

热点排行