我试图使用TCP套接字实现一种非常简单的NFS形式。除了"ls"命令之外,所有功能都正常工作。我的计划是使用链表结构传递当前目录中组成文件和目录名称列表。我编写了以下代码:
最初的回答:
struct lnode
{
char name[256];
struct lnode* next;
};
DIR* drptr = opendir("."); //as of now, only current directory is considered
if(drptr==NULL)
{
perror("Could not open");
}
else
{
struct dirent* drnt;
struct lnode* head = NULL,*tail = NULL;
drnt = readdir(drptr);
while(drnt!=NULL)
{
if(strcmp(drnt->d_name,".")!=0&&strcmp(drnt->d_name,"..")!=0)
{
if(head==NULL)
{
head = (struct lnode*)malloc(sizeof(struct lnode));
strcpy(head->name,drnt->d_name);
head->next = NULL;
teail = head;
}
else
{
tail->next = (struct lnode*)malloc(sizeof(struct lnode));
strcpy(tail->next->name,drnt->d_name);
tail->next->next = NULL;
tail = tail->next;
}
}
else
{
break;
}
drnt = readdir(drptr);
}
write(1,head,sizeof(lnode)); // the socket is duped to 1, so 1 is used for socket communication
}
在客户端,我是这样读取的:
最初的回答:
struct lnode* l,*q;
recv(sfd,l,sizeof(struct lnode),0);
q = l;
while(q!=NULL)
{
printf("%s\n",q->name);
q = q->next;
}
这里我遇到了段错误。经过思考,我了解到“next”指针指向服务器程序地址空间中的地址,因此客户端无法访问它。所以我使用字符串数组传递d_name列表,显然,这个方法完美地解决了问题。
所以,我的问题是:
1. 有没有办法通过套接字连接传递链接列表? 2. 如果不行,那最佳的方法是如何在网络上传递文件和目录列表?在真正的NFS中如何实现?