佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1220|回复: 3

如何用stat()在linux Cprogram

[复制链接]
发表于 23-8-2010 05:05 PM | 显示全部楼层 |阅读模式

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
因为连简单的例子我都找不到,所以在这里求助
请大家提供一些讯息谢谢
回复

使用道具 举报


ADVERTISEMENT

发表于 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 | 显示全部楼层
  1. #include  <stdio.h>
  2. #include  <sys/types.h>
  3. #include  <sys/stat.h>
  4. #include  <dirent.h>
  5. #include  <unistd.h>

  6. static void list_files(const char *dirname)
  7. {
  8.     char  fullname[MAXPATHLEN];

  9.     .....
  10.     while((dp = readdir(dirp)) != NULL) {  

  11.         struct stat  stat_buffer;

  12.         sprintf(fullname, "%s/%s", dirname, dp->d_name );   

  13.         if(stat(fullname, &stat_buffer) != 0) {
  14.              perror( progname );
  15.         }
  16.         else if( S_ISDIR( stat_buffer.st_mode )) {
  17.             printf( "%s is a directory\n", fullname );
  18.         }
  19.         else if( S_ISREG( stat_buffer.st_mode )) {
  20.             printf( "%s is a regular file\n", fullname );
  21.         }
  22.         else {
  23.             printf( "%s is weird!\n", fullname );
  24.         }
  25.     }
  26.     closedir(dirp);
  27. }
复制代码


这是一个简单的例子,其他的可以在网上看manual
回复

使用道具 举报

发表于 4-9-2010 02:57 PM | 显示全部楼层
本帖最后由 FOSS 于 4-9-2010 03:00 PM 编辑

顺便介绍你一个socket programming的网站,很有用的
http://beej.us/guide/bgnet/output/html/multipage/index.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 21-11-2025 02:32 PM , Processed in 0.137137 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表