UNIX网络编程第一次作业基本搞定

Programming Assignment #1<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>

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:

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /?>

as1.gif
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
None.gif # include  < stdio.h >
None.gif# include 
< sys / types.h >
None.gif# include 
< sys / stat.h >
None.gif# include 
< fcntl.h >
None.gif# include 
" unistd.h "
None.gif# include 
< string .h >
None.gif# include 
< limits.h >
None.gif
None.gif
#define  PUBLIC "/tmp/PUBLIC"
None.gif
#define  B_SIZ ( PIPE_BUF/2 )
None.gif
struct  Message
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
char  fifo_name[B_SIZ];
InBlock.gif    
char  cmd_line[B_SIZ];
ExpandedBlockEnd.gif}
;
None.gif

server.c

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

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

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

ExpandedBlockEnd.gif}

None.gif

client.c

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

ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**/ /* open public FIFO to write  */
InBlock.gif     
if ( (publicfifo  =  open(PUBLIC,O_WRONLY)) ==- 1 ) // 打开一个文件用于读或写 
ExpandedSubBlockStart.gifContractedSubBlock.gif
      dot.gif {
InBlock.gif         perror(PUBLIC);
InBlock.gif         exit(
2 );
ExpandedSubBlockEnd.gif     }
 
InBlock.gif     
while ( 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif {
InBlock.gif
InBlock.gif          write(fileno(stdout),
" \n send client cmd> " , 18 );  // 将提示信息写到标准输出
InBlock.gif
          memset(msg.cmd_line, 0x0 ,B_SIZ);  // 清除命令
InBlock.gif
          n  =  read(fileno(stdin),msg.cmd_line,B_SIZ);  // 从输入读入命令到msg.cmd_line
InBlock.gif
           if ( ! strncmp( " quit " ,msg.cmd_line,n - 1 ))  // 是否退出
InBlock.gif
            break ;
InBlock.gif          write(publicfifo,(
char * ) & msg, sizeof (msg));  // 将命令写入到公共FIFO
InBlock.gif
InBlock.gif          
InBlock.gif          
// 打开私有FIFO
InBlock.gif
           if ((privatefifo  =  open(msg.fifo_name,O_RDONLY)) ==- 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif {
InBlock.gif              perror(msg.fifo_name);
InBlock.gif              exit(
3 );
ExpandedSubBlockEnd.gif          }

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

InBlock.gif          close(privatefifo);
ExpandedSubBlockEnd.gif    }

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

None.gif


      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值