Chinaunix首页 | 论坛 | 博客
  • 博客访问: 407348
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: LINUX

2014-10-24 09:58:08



代码下载:



client.c

  1. #include<sys/ipc.h>
  2. #include<sys/msg.h>
  3. #include<string.h>
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<fcntl.h>
  7. #include<unistd.h>
  8. #include<errno.h>
  9. #include"myinclude.h"

  10. int main()
  11. {
  12.     int msqid;
  13.     size_t len;
  14.     ssize_t n;
  15.     char *ptr;
  16.     struct mymesg mesg;

  17.     if((msqid=msgget(MQ_KEY,0))==-1)
  18.         err_sys("can't create the msgqid");

  19.     snprintf(mesg.mesg_data,MAXMESGDATA,"%ld ",(long)getpid());
  20.     len=strlen(mesg.mesg_data);
  21.     ptr=mesg.mesg_data+len;

  22.     fgets(ptr,MAXMESGDATA-len,stdin);
  23.     len=strlen(mesg.mesg_data);
  24.     if(mesg.mesg_data[len-1]=='\n')
  25.         len--;
  26.     mesg.mesg_len=len;
  27.     mesg.mesg_type=1;

  28.     msgsnd(msqid,&(mesg.mesg_type),mesg.mesg_len,0);
  29.     mesg.mesg_type=getpid();
  30.     while((n=msgrcv(msqid,&(mesg.mesg_type),MAXMESGDATA,mesg.mesg_type,0))>0)
  31.     {    
  32.         write(STDOUT_FILENO,mesg.mesg_data,n);
  33.     }

  34.     exit(0);
  35. }

server.c

  1. #include<sys/ipc.h>
  2. #include<sys/msg.h>
  3. #include<string.h>
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<fcntl.h>
  7. #include<errno.h>
  8. #include"myinclude.h"

  9. int main()
  10. {
  11.     int msqid;
  12.     FILE *fp;
  13.     char *ptr;
  14.     pid_t pid;
  15.     ssize_t n;
  16.     struct mymesg mesg;

  17.     if((msqid=msgget(MQ_KEY,SVMSG_MODE |IPC_CREAT))==-1)
  18.         err_sys("can't create the msgqid");
  19.     for(;;)
  20.     {
  21.         mesg.mesg_type=1;
  22.         if((n=msgrcv(msqid,&(mesg.mesg_type),MAXMESGDATA,mesg.mesg_type,0))==0)
  23.         {
  24.             err_msg("path missing");
  25.             continue;
  26.         }

  27.         mesg.mesg_data[n]='\0';

  28.         if((ptr=strchr(mesg.mesg_data,' '))==NULL)
  29.         {
  30.             err_msg("bogus request: %s",mesg.mesg_data);
  31.             continue;
  32.         }

  33.         *ptr++=0;
  34.         pid=atol(mesg.mesg_data);
  35.         mesg.mesg_type=pid;

  36.         if((fp=fopen(ptr,"r"))==NULL)
  37.         {
  38.             snprintf(mesg.mesg_data+n,sizeof(mesg.mesg_data)-n,":can't open,%s\n",strerror(errno));
  39.             mesg.mesg_len=strlen(ptr);
  40.             memmove(mesg.mesg_data,ptr,mesg.mesg_len);
  41.             msgsnd(msqid,&(mesg.mesg_type),mesg.mesg_len,0);
  42.         }
  43.         else
  44.         {
  45.             while(fgets(mesg.mesg_data,MAXMESGDATA,fp)!=NULL)
  46.             {
  47.                 mesg.mesg_len=strlen(mesg.mesg_data);
  48.                 msgsnd(msqid,&(mesg.mesg_type),mesg.mesg_len,0);
  49.             }
  50.             fclose(fp);
  51.         }
  52.         mesg.mesg_len=0;
  53.         msgsnd(msqid,&(mesg.mesg_type),mesg.mesg_len,0);
  54.     }

  55.     exit(0);
  56. }

error.c

  1. #include "myinclude.h"    /* for definition of errno */
  2. #include<stdarg.h>        /* ISO C variable aruments */
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include<errno.h>

  7. #define MAXLINE 4096

  8. static void    err_doit(int, int, const char *, va_list);

  9. /*
  10.  * Nonfatal error related to a system call.
  11.  * Print a message and return.
  12.  */
  13. void
  14. err_ret(const char *fmt, ...)
  15. {
  16.     va_list        ap;

  17.     va_start(ap, fmt);
  18.     err_doit(1, errno, fmt, ap);
  19.     va_end(ap);
  20. }

  21. /*
  22.  * Fatal error related to a system call.
  23.  * Print a message and terminate.
  24.  */
  25. void
  26. err_sys(const char *fmt, ...)
  27. {
  28.     va_list        ap;

  29.     va_start(ap, fmt);
  30.     err_doit(1, errno, fmt, ap);
  31.     va_end(ap);
  32.     exit(1);
  33. }

  34. /*
  35.  * Fatal error unrelated to a system call.
  36.  * Error code passed as explict parameter.
  37.  * Print a message and terminate.
  38.  */
  39. void
  40. err_exit(int error, const char *fmt, ...)
  41. {
  42.     va_list        ap;

  43.     va_start(ap, fmt);
  44.     err_doit(1, error, fmt, ap);
  45.     va_end(ap);
  46.     exit(1);
  47. }

  48. /*
  49.  * Fatal error related to a system call.
  50.  * Print a message, dump core, and terminate.
  51.  */
  52. void
  53. err_dump(const char *fmt, ...)
  54. {
  55.     va_list        ap;

  56.     va_start(ap, fmt);
  57.     err_doit(1, errno, fmt, ap);
  58.     va_end(ap);
  59.     abort();        /* dump core and terminate */
  60.     exit(1);        /* shouldn't get here */
  61. }

  62. /*
  63.  * Nonfatal error unrelated to a system call.
  64.  * Print a message and return.
  65.  */
  66. void
  67. err_msg(const char *fmt, ...)
  68. {
  69.     va_list        ap;

  70.     va_start(ap, fmt);
  71.     err_doit(0, 0, fmt, ap);
  72.     va_end(ap);
  73. }

  74. /*
  75.  * Fatal error unrelated to a system call.
  76.  * Print a message and terminate.
  77.  */
  78. void
  79. err_quit(const char *fmt, ...)
  80. {
  81.     va_list        ap;

  82.     va_start(ap, fmt);
  83.     err_doit(0, 0, fmt, ap);
  84.     va_end(ap);
  85.     exit(1);
  86. }

  87. /*
  88.  * Print a message and return to caller.
  89.  * Caller specifies "errnoflag".
  90.  */
  91. static void
  92. err_doit(int errnoflag, int error, const char *fmt, va_list ap)
  93. {
  94.     char    buf[MAXLINE];

  95.     vsnprintf(buf, MAXLINE, fmt, ap);
  96.     if (errnoflag)
  97.         snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
  98.          strerror(error));
  99.     strcat(buf, "\n");
  100.     fflush(stdout);        /* in case stdout and stderr are the same */
  101.     fputs(buf, stderr);
  102.     fflush(NULL);        /* flushes all stdio output streams */
  103. }


myinclude.h

  1. #ifndef MYINCLUDE_H
  2. #define MYINCLUDE_H
  3. #include<fcntl.h>
  4. /*定义一些常量*/
  5. #define MAXLINE 4096/*max line length*/
  6. #define FILE_MODE (S_IRUSR |S_IWUSR |S_IRGRP |S_IROTH)/*文件权限*/

  7. #define MQ_KEY 1234L
  8. #define MAXMESGDATA 1024
  9. #define SVMSG_MODE 0644

  10. /*在出错处理函数error.c里的一些原型*/
  11. void err_ret(const char *fmt, ...);
  12. void err_sys(const char *fmt, ...);
  13. void err_exit(int,const char *fmt, ...);
  14. void err_dump(const char *fmt, ...);
  15. void err_quit(const char *fmt, ...);

  16. struct mymesg
  17. {
  18.     long    mesg_len;    /* #bytes in mesg_data, can be 0 */
  19.     long    mesg_type;    /* message type, must be > 0 */
  20.     char    mesg_data[MAXMESGDATA];
  21. };

  22. #endif





阅读(1948) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~