| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4177 人关注过本帖
标题:基于进程间通讯的一个简单的客户端服务器模型
只看楼主 加入收藏
绝笔题痕
Rank: 1
等 级:新手上路
帖 子:3
专家分:2
注 册:2015-1-2
收藏
 问题点数:0 回复次数:0 
基于进程间通讯的一个简单的客户端服务器模型
程序代码:
/*************************************************************************
    > File Name: my_server.c
    > Author: octopus
    > Mail: octopus_work. 
    > Created Time: Wed 04 Feb 2015 05:07:45 AM PST
 ************************************************************************/

#include "my_server.h"
//服务器端


void handle(char *src){

         int bg;
         int end;
         int tmp;
         bg=0;
         end=strlen(src)-1;
         while(bg<end){
               tmp=src[bg];
               src[bg]=src[end];
               src[end]=tmp;
               bg++;
               end--;
         }
}

void child_main(int fd_cr,int fd_cw){

      char  msg[1024];
      while(memset(msg,0,1024),read(fd_cr,msg,1024)!=0){
          handle(msg);
          write(fd_cw,msg,strlen(msg));
      }

}

int  main(int argc,char * argv[] ){

    int fd_server;    //open fifo
   char path_name[128];
   char  fifo_name[128];
   char  buf[1024];
   FILE *fp;
   int client_pid;
   char  readfile[128];    //client read
   char  writefile[128];    //client write
   
   int fd_cr;   //read fifo describle
   int fd_cw ;   //write  fifo describle 
 

   memset(path_name,0,128);
 //memset(fifo_name,0,128);
   sprintf(path_name,"%s/%s",PATH,FIFO_NAME);
   mkfifo(path_name,0666);
   printf("mkfifo over\n");

    fd_server=open(path_name,O_RDONLY);
    open(path_name,O_WRONLY);
    while(fd_server==-1){
    printf("open fail\n");    //open server fifo
        exit(1);
    }
    fp=fdopen(fd_server,"r");
    while(memset(buf,0,1024),fgets(buf,1024,fp)!=NULL){   //getpid()
     
            sscanf(buf,"%d",&client_pid);   
            printf("%d  online\n",client_pid);  //client  online  
            memset(readfile,0,128);
            memset(writefile,0,128);
            
            sprintf(readfile,"%s/%d_r.fifo",PATH,client_pid);
            sprintf(writefile,"%s/%d_w.fifo",PATH,client_pid);
             
            printf("arrived  here\n");
            printf("%s\n",readfile);
            printf("%s\n",writefile);
           
            fd_cr=open(writefile,O_RDONLY);                      /* 问题一: 此处两句通不过,管道打不开,我仔细研究过
                fd_cw=open(readfile,O_WRONLY);                                   不明白为什么程序为卡在这?  */
          //    fd_cr=open(cw,O_RDONLY);
            printf("open  file  successful\n");
            
            if(fork()==0){
                  
                    child_main(fd_cr,fd_cw);
                    close(fd_cr);
                    close(fd_cw);
                    exit(1);
            }
                   close(fd_cr);
                   close(fd_cw);

        }
                memset(fifo_name,0,128);
                sprintf(fifo_name,"%s/%s",PATH,FIFO_NAME);
                unlink(fifo_name);
                 return 0;
}








/*************************************************************************
    > File Name: my_client.c
    > Author: octopus
    > Mail: octopus_work.
    > Created Time: Wed 04 Feb 2015 05:47:09 AM PST
 ************************************************************************/

#include"my_server.h"

//客户端端
int main(int argc ,char * argv[]){     //shm_key,sem_key

       key_t   shm_key,sem_key;
       int    my_shm,my_sem;
       char line[1024];
       char  buf[32];
       char readfile[128];
       char writefile[128];
       char  path_name[128];
       char  fifo_name[128];
       int  fd_server;
       int  fd_cr;
       int  fd_cw;
      
       memset(fifo_name,0,128);             //creat  r.fifo
       sprintf(fifo_name,"%s/%d_r.fifo",PATH,getpid());
       sprintf(readfile,"%s/%d_r.fifo",PATH,getpid());
      // printf("to here\n");
        if(mkfifo(fifo_name,0666)==-1){
           printf("mkfifo fail\n");
           exit(1);
        }
      
      
       memset(fifo_name,0,128);                //create  w.fifo
       sprintf(fifo_name,"%s/%d_w.fifo",PATH,getpid());
       sprintf(writefile,"%s/%d_w.fifo",PATH,getpid());
       if(mkfifo(fifo_name,0666)==-1){
          printf("mkfifo  fail\n");
          exit(1);
      }

         
       memset(path_name,0,1024);  
       sprintf(path_name,"%s/%s",PATH,FIFO_NAME);
       fd_server=open(path_name,O_WRONLY);
       if(fd_server==-1){
           printf("open fail\n");
           exit(1);
       }
      // printf("fifo  creat  successful\n");
      
       memset(buf,0,32);
       sprintf(buf,"%d\n",getpid());
       write(fd_server,buf,strlen(buf));  //write  process  id  
      
       printf("arrive\n");

      /* memset(fifo_name,0,128);
       sprintf(fifo_name,"%s/%d_r.fifo",PATH,getpid());
       fd_cr=open(fifo_name,O_RDONLY);
       if(fd_cr==-1){
           printf("open fail \n");
           exit(1);
       }
     // printf("----------\n");
      memset(fifo_name,0,128);
      sprintf(fifo_name,"%s/%d_w.fifo",PATH,getpid());
      fd_cw=open(fifo_name,O_WRONLY);
      if(fd_cw==-1){
          printf("open fail\n");
          exit(1);
      }*/
           fd_cr=open(readfile,O_RDONLY);         I                            /*  问题二: 被注解的上面几句修改为这两句,管道才能打开 */
       fd_cw=open(writefile,O_WRONLY);                                     /*  上面那样打开有问题么?为什么通不过*/
      
       printf("weather arrive  here \n ");
       pMBUF   p;
       shm_key=(key_t)atoi(argv[1]);
       sem_key=(key_t)atoi(argv[2]);

       my_shm=shmget(shm_key,sizeof(MBUF),0666|IPC_CREAT);  //creat
       my_sem=semget(sem_key,1,0666|IPC_CREAT);    //creat lock   set signal 1
   
       semctl(my_sem,0,SETVAL,1);

      p=(pMBUF)shmat(my_shm,NULL,0);
      memset(p,0,sizeof(MBUF));

      while(memset(line,0,1024),fgets(line,1024,stdin)!=NULL){
                 printf("start  write\n");               
                 write(fd_cw,line,strlen(line));
               
                //    sleep(1);
                 memset(line,0,1024);
                 read(fd_cr,line,1024);
                 printf(" line :  %s \n",line);
               while(P(my_sem),p->m_flag==1){
                    
                      V(my_sem);
                      sleep(1);
            }
            strcpy(p->m_buf,line);
            p->m_flag=1;
            V(my_sem);


      }

      while(P(my_sem),p->m_flag==1)
      {
            V(my_sem);
            sleep(1);
      }

         strcpy(p->m_buf,"over");
         p->m_flag=1;
         V(my_sem);
         sleep(3);

        shmdt(p);                          //unlink  
        shmctl(my_shm,IPC_RMID,NULL);   //close  memery
        semctl(my_sem,0,IPC_RMID);       //close signal


}






问题描述:  楼主的疑惑之处已在代码中/* 处显示出来,上述并非全部实现代码,恳请各位大神显现神通,这几个问题我已经盯了好长时间。
搜索更多相关主题的帖子: include 服务器 客户端 模型 
2015-02-06 11:40
快速回复:基于进程间通讯的一个简单的客户端服务器模型
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025821 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved