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

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
}


      

posted on 2006-04-01 15:03  秋雨飘飞  阅读(1153)  评论(0编辑  收藏  举报