69,369
社区成员
发帖
与我相关
我的任务
分享
struct dirent {
ino_t d_ino; /* 索引号 */
off_t d_off; /* 下一个偏移量 */
unsigned short d_reclen; /* 本记录长度 */
unsigned char d_type; /* 文件类型 */
char d_name[256]; /* 文件名 */
};
DIR *dir;
struct dirent *ptr;
char dirbuf[sizeof(*ptr)];
if((dir = opendir("./")) == NULL) {
perror("opendir\n");
exit(1);
}
while((ptr = readdir(dir)) != NULL) {
memcpy(dirbuf, &ptr, sizeof(*ptr));
if((sendBytes = send(clientfd, dirbuf, sizeof(dirbuf), 0)) == -1) {
perror("fail to send dir");
exit(1);
}
printf("name:%s\n", ptr->d_name);
}
closedir(dir);
struct dirent *ptr;
char dirbuf[sizeof(*ptr)];
printf("current dir on server:\n");
memset(dirbuf, 0x00, sizeof(dirbuf));
while(recvBytes = recv(sockfd, dirbuf, sizeof(dirbuf), 0)) {
if(recvBytes == -1) {
printf("faile to receive dir");
exit(1);
}
memcpy(&ptr, dirbuf, sizeof(*ptr));
printf("%s\n", ptr->d_name);
}