【发布时间】:2011-09-07 04:25:17
【问题描述】:
我目前正在 Unix/Windows 环境中制作客户端和服务器,但现在我只是在 Unix 方面工作。我们必须为程序创建的函数之一类似于 Unix 中的 list 函数,它显示目录中的所有文件,但我们还必须显示有关文件的更多信息,例如其所有者和创建日期。现在我能够获取所有这些信息并将其打印给客户端,但是我们还必须补充一点,一旦程序打印了 40 行,它会等待客户端在继续打印之前按下任何键。
我必须让程序来做这件事,但它会导致我的客户端和服务器不同步,或者至少标准输出不同步。这意味着如果我输入命令“asdad”,它应该打印无效命令,但在我输入另一个命令之前它不会打印该消息。我在下面添加了我的列表功能代码。我愿意接受有关如何完成此要求的建议,因为我选择的方法似乎没有奏效。
提前谢谢你。
Server - Fork 功能:输入 list 命令时调用。例如
fork_request(newsockfd, "list", buf);
int fork_request(int fd, char req[], char buf[])
{
#ifndef WIN
int pid = fork();
if (pid ==-1)
{
printf("Failed To Fork...\n");
return-1;
}
if (pid !=0)
{
wait(NULL);
return 10;
}
dup2(fd,1); //redirect standard output to the clients std output.
close(fd); //close the socket
execl(req, req, buf, NULL); //run the program
exit(1);
#else
#endif
}
这是用于获取有关目录中文件的所有信息的函数
void longOutput(char str[])
{
char cwd[1024];
DIR *dip;
struct dirent *dit;
int total;
char temp[100];
struct stat FileAttrib;
struct tm *pTm;
int fileSize;
int lineTotal;
if(strcmp(str, "") == 0)
{
getcwd(cwd, sizeof(cwd));
}
else
{
strcpy (cwd, str);
}
if (cwd != NULL)
{
printf("\n Using Dir: %s\n", cwd);
dip = opendir(cwd);
if(dip != NULL)
{
while ((dit = readdir(dip)) != NULL)
{
printf("\n%s",dit->d_name);
stat(dit->d_name, &FileAttrib);
pTm = gmtime(&FileAttrib.st_ctime);
fileSize = FileAttrib.st_size;
printf("\nFile Size: %d Bytes", fileSize);
printf("\nFile created on: %.2i/%.2i/%.2i at %.2i:%.2i:%.2i GMT \n", (pTm->tm_mon + 1), pTm->tm_mday,(pTm->tm_year % 100),pTm->tm_hour,pTm->tm_min, pTm->tm_sec);;
lineTotal = lineTotal + 4;
if(lineTotal == 40)
{
printf("40 Lines: Waiting For Input!");
fflush(stdout);
gets(&temp);
}
}
printf("\n %d \n", lineTotal);
}
else
{
perror ("");
}
}
}
这里是我检查的客户端部分!在返回的消息中找不到。如果有,则表示要打印更多行。
if(strchr(command,'!') != NULL)
{
char temp[1000];
gets(&temp);
}
抱歉,这篇文章很长,但如果您需要任何东西,请尽管询问。
【问题讨论】:
-
仅供参考,在 UNIX 上
st_ctime是文件的“更改”时间(文件的元数据最后一次更改),而不是创建时间。 UNIX 不跟踪文件的创建时间。 -
嘿加布。我知道这一点,但目前只使用 st_ctime 就可以了。
标签: unix networking fork stdout