|
查看: 1220|回复: 3
|
如何用stat()在linux Cprogram
[复制链接]
|
|
|
|
Identifyall the features established when the connection is made using thefollowing code. What is the main function used to establish theconnection?
int main(int argc, char *argv[ ])
{
char *service = "echo"; /* service name or port number */
struct sockaddr_in fsin; /* the address of a client */
int alen; /* length of client's address */
int msock; /* master server socket */
int ssock; /* slave server socket */
msock = passiveTCP(service, QLEN);
signal(SIGCHLD, reaper);
while (1)
{
alen = sizeof(fsin);
ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
if(ssock < 0) {
if(errno == EINTR)
continue;
errexit("accept: %s\n", strerror(errno));
}
switch (fork())
{
/* child */
case 0:
close(msock);
exit(TCPechod(ssock));
/* parent */
default:
close(ssock);
break;
case -1:
errexit("fork: %s\n", strerror(errno));
}
}
}
int TCPechod(int fd)
{
char buf[BUFSIZ];
int cc;
while (cc = read(fd, buf, sizeof buf))
{
if(cc < 0)
errexit("echo read: %s\n", strerror(errno));
if(write(fd, buf, cc) < 0)
errexit("echo write: %s\n", strerror(errno));
}
return 0;
}
void reaper(int sig)
{
int status;
while (wait3(&status, WNOHANG, (struct rusage *)0) >= 0)
/* empty ;*/
}
请问有人明白题目要的是什么?能不能解释给小第听, 谢谢
还有就是
如何用stat()在linux Cprogram 去 display file system information
因为连简单的例子我都找不到,所以在这里求助
请大家提供一些讯息谢谢 |
|
|
|
|
|
|
|
|
|
|
发表于 4-9-2010 02:50 PM
|
显示全部楼层
题目是要问你怎样来establish conn
例如你用什么socket,什么transport layer,是 server/client?
注意几个 keyword: accept, bind, listen, read, write
还有注意几个while loop,为什么要用while loop? hint: max message size
还有为什么要用 fork()?什么是 fork()? |
|
|
|
|
|
|
|
|
|
|
发表于 4-9-2010 02:54 PM
|
显示全部楼层
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <unistd.h>
- static void list_files(const char *dirname)
- {
- char fullname[MAXPATHLEN];
- .....
- while((dp = readdir(dirp)) != NULL) {
- struct stat stat_buffer;
- sprintf(fullname, "%s/%s", dirname, dp->d_name );
- if(stat(fullname, &stat_buffer) != 0) {
- perror( progname );
- }
- else if( S_ISDIR( stat_buffer.st_mode )) {
- printf( "%s is a directory\n", fullname );
- }
- else if( S_ISREG( stat_buffer.st_mode )) {
- printf( "%s is a regular file\n", fullname );
- }
- else {
- printf( "%s is weird!\n", fullname );
- }
- }
- closedir(dirp);
- }
复制代码
这是一个简单的例子,其他的可以在网上看manual |
|
|
|
|
|
|
|
|
|
|
发表于 4-9-2010 02:57 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|