惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
博客园 - 司徒正美
L
LangChain Blog
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
Microsoft Security Blog
Microsoft Security Blog
Cyberwarzone
Cyberwarzone
Project Zero
Project Zero
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
T
Tenable Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Troy Hunt's Blog
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Scott Helme
Scott Helme
Recent Announcements
Recent Announcements
S
Secure Thoughts
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
Hacker News - Newest:
Hacker News - Newest: "LLM"
雷峰网
雷峰网
Attack and Defense Labs
Attack and Defense Labs
A
About on SuperTechFans
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
Martin Fowler
Martin Fowler
V2EX - 技术
V2EX - 技术
S
Securelist
Microsoft Azure Blog
Microsoft Azure Blog
N
Netflix TechBlog - Medium
B
Blog RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
PCI Perspectives
PCI Perspectives
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hacker News: Front Page

博客园 - 秋雨飘飞

Data type mismatch in criteria expression. 条件表达式中数据类型不匹配 PInvoke 知识记录 近两年的BLOG博龄 前天一道我不能回答好的面试题:内存泄露你怎么解决?希望大家不吝赐教 ACCESS模糊查询出现的变态问题,不知道该问题的希望注意,知道内幕的高手还望给小弟一个解释 Thanks logahead - AJAX的BLOG AJAX淋漓尽致的发挥(Google个性化主页 VS. Windows Live.COM)站在互联网浪尖上窃喜 汉诺塔 - 秋雨飘飞 - 博客园 原来BT也要设置端口映射的,今天才发现 dup,dup2函数 恶心的C语言strtok函数 发布这几天学习Hook搞出来的一个挺好玩的统计鼠标移动距离和键盘敲击次数的小程序 像素真实的物理长度 学习笔记-HOOK钩子(1)l 监视剪贴板 捕获网页为图像 2D绘图控件 趋势程序大赛第八天 趋势程序大赛第 六&&七 天
UNIX网络编程第一次作业基本搞定
秋雨飘飞 · 2006-04-01 · via 博客园 - 秋雨飘飞

Programming Assignment #1

Problem Description:

Write two separate programs for such a client-server model: the client reads a UNIX command from keyboard, writes it to the IPC channel, reads the command execution result and displays it on screen. The server reads the command from the IPC channel, executes it, and writes the execution result to the IPC channel. The whole procedure is shown as follows:


To run your program, please first start the server process in the background like server& and then run the client in the foreground since the client reads from the standard input and writes to the standard output. Alternatively you can open 2 terminal windows respectively for the server and the client.

Requirements:

1)  You need to use 2 FIFOs to implement the IPC channels since the client and the server run in two unrelated processes.

2)  The server may use popen() to execute a UNIX command and get the execution result. Pls pay special attention to the command “cd”.

3)       Your program should be able to handle errors. For example, if the client sends an illegal Unix command, the server will return the Unix original error message. The client then displays the error message. 

4)       Send your assignment to the corresponding TA’s email account.

5)       Late submission is not accepted.

      先贴到BLOG上
      只是作业要求有点不是非常明白
      CD命令执行没有得出结果,原因上课的时候好像也讲了,但是要求我们程序中进行什么处理呢?
      不解啊。。。。

附源程序:

header.h

# include  < stdio.h >
# include 
< sys / types.h >
# include 
< sys / stat.h >
# include 
< fcntl.h >
# include 
" unistd.h "
# include 
< string .h >
# include 
< limits.h >

#define  PUBLIC "/tmp/PUBLIC"
#define  B_SIZ ( PIPE_BUF/2 )
struct  Message
{
    
char  fifo_name[B_SIZ];
    
char  cmd_line[B_SIZ];
}
;

server.c

# include  " header.h "
main(
void )
{
    
int  n,done,dummyfifo,publicfifo,privatefifo;
    
struct  Message msg;
    FILE 
*  fin;
    
static   char  buffer[PIPE_BUF];
    
/* 生成公共FIFO  */
    mknod  ( PUBLIC, S_IFIFO
| 0666 0  );
    
/* 打开公共FIFO读写  */
    
if  ( ( publicfifo  =  open (PUBLIC,O_RDONLY) ) ==- 1   ||
        ( dummyfifo 
==  open (PUBLIC,O_WRONLY | O_NDELAY) ) ==- 1  )
    
{
        perror(PUBLIC);
        exit(
1 );
    }

    printf(
" Server has been started\n PUBLIC FIFO:%s\n " ,PUBLIC);
    
/*  从公共FIFO读取 */
    
while  ( read( publicfifo, ( char * ) & msg, sizeof (msg)) > 0  )
    
{
        n
= done = 0 ;
        
do
            
/* 打开client私有FIFO----sleep参考某本书上的做法,书名记不清楚了 */
          
if ( (privatefifo  =  open(msg.fifo_name,O_WRONLY | O_NDELAY)) ==- 1 )
            sleep(
3 );
          
else
          
{
           fin 
=  popen(msg.cmd_line, " r " );  /* 执行客户端的命令  */
           write ( privatefifo , 
" \n "  ,  1  );
           
while ( (n  =  read (fileno(fin),buffer,PIPE_BUF) ) > 0 )
           
{
                write(privatefifo,buffer,n); 
/* write to private fifo */
                memset(buffer,
0x0 ,PIPE_BUF);  /* empty the buffer */
           }

           pclose(fin);
           close(privatefifo);    
           done 
=   1 ;
          }
    
        }
while ( ++ n < 5   &&   ! done);
        
if (done  ==   0 /*  fail */
         write(fileno(stderr),
" \n NOTE:Server never accessed private FIFO \n " , 481 );
    }

}

client.c

# include  " header.h "

int  main( void {
    
int  n,privatefifo,publicfifo;
    
static   char  buffer[PIPE_BUF];
    
struct  Message msg;
    
/* 生成私有FIFO的名称  */     
    sprintf( msg.fifo_name, 
" /tmp/fifo%d "  , getpid()); // 送格式化输出到字符串中 
      /* 生成私有FIFO  */
     
if ( mknod( msg.fifo_name, S_IFIFO | 0666  ,  0 ) < 0  )
     
{
         perror(msg.fifo_name);    
// 系统错误信息    
         exit( 1 );
     }

     
/* open public FIFO to write  */
     
if ( (publicfifo  =  open(PUBLIC,O_WRONLY)) ==- 1 ) // 打开一个文件用于读或写 
      {
         perror(PUBLIC);
         exit(
2 );
     }
 
     
while ( 1 )
     
{

          write(fileno(stdout),
" \n send client cmd> " , 18 );  // 将提示信息写到标准输出
          memset(msg.cmd_line, 0x0 ,B_SIZ);  // 清除命令
          n  =  read(fileno(stdin),msg.cmd_line,B_SIZ);  // 从输入读入命令到msg.cmd_line
           if ( ! strncmp( " quit " ,msg.cmd_line,n - 1 ))  // 是否退出
            break ;
          write(publicfifo,(
char * ) & msg, sizeof (msg));  // 将命令写入到公共FIFO

          
          
// 打开私有FIFO
           if ((privatefifo  =  open(msg.fifo_name,O_RDONLY)) ==- 1 )
          
{
              perror(msg.fifo_name);
              exit(
3 );
          }


          
// 读取私有FIFO的消息到buffer,然后输出buffer
           while ( (n = read(privatefifo,buffer,PIPE_BUF)) > 0  )
          
{
              write(fileno(stderr),buffer,n);
          }

          close(privatefifo);
    }

    close(publicfifo);
    unlink(msg.fifo_name);
// delete
}