
代码下载:
client.c
-
#include<sys/ipc.h>
-
#include<sys/msg.h>
-
#include<string.h>
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<fcntl.h>
-
#include<unistd.h>
-
#include<errno.h>
-
#include"myinclude.h"
-
-
int main()
-
{
-
int msqid;
-
size_t len;
-
ssize_t n;
-
char *ptr;
-
struct mymesg mesg;
-
-
if((msqid=msgget(MQ_KEY,0))==-1)
-
err_sys("can't create the msgqid");
-
-
snprintf(mesg.mesg_data,MAXMESGDATA,"%ld ",(long)getpid());
-
len=strlen(mesg.mesg_data);
-
ptr=mesg.mesg_data+len;
-
-
fgets(ptr,MAXMESGDATA-len,stdin);
-
len=strlen(mesg.mesg_data);
-
if(mesg.mesg_data[len-1]=='\n')
-
len--;
-
mesg.mesg_len=len;
-
mesg.mesg_type=1;
-
-
msgsnd(msqid,&(mesg.mesg_type),mesg.mesg_len,0);
-
mesg.mesg_type=getpid();
-
while((n=msgrcv(msqid,&(mesg.mesg_type),MAXMESGDATA,mesg.mesg_type,0))>0)
-
{
-
write(STDOUT_FILENO,mesg.mesg_data,n);
-
}
-
-
exit(0);
- }
server.c
-
#include<sys/ipc.h>
-
#include<sys/msg.h>
-
#include<string.h>
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<fcntl.h>
-
#include<errno.h>
-
#include"myinclude.h"
-
-
int main()
-
{
-
int msqid;
-
FILE *fp;
-
char *ptr;
-
pid_t pid;
-
ssize_t n;
-
struct mymesg mesg;
-
-
if((msqid=msgget(MQ_KEY,SVMSG_MODE |IPC_CREAT))==-1)
-
err_sys("can't create the msgqid");
-
for(;;)
-
{
-
mesg.mesg_type=1;
-
if((n=msgrcv(msqid,&(mesg.mesg_type),MAXMESGDATA,mesg.mesg_type,0))==0)
-
{
-
err_msg("path missing");
-
continue;
-
}
-
-
mesg.mesg_data[n]='\0';
-
-
if((ptr=strchr(mesg.mesg_data,' '))==NULL)
-
{
-
err_msg("bogus request: %s",mesg.mesg_data);
-
continue;
-
}
-
-
*ptr++=0;
-
pid=atol(mesg.mesg_data);
-
mesg.mesg_type=pid;
-
-
if((fp=fopen(ptr,"r"))==NULL)
-
{
-
snprintf(mesg.mesg_data+n,sizeof(mesg.mesg_data)-n,":can't open,%s\n",strerror(errno));
-
mesg.mesg_len=strlen(ptr);
-
memmove(mesg.mesg_data,ptr,mesg.mesg_len);
-
msgsnd(msqid,&(mesg.mesg_type),mesg.mesg_len,0);
-
}
-
else
-
{
-
while(fgets(mesg.mesg_data,MAXMESGDATA,fp)!=NULL)
-
{
-
mesg.mesg_len=strlen(mesg.mesg_data);
-
msgsnd(msqid,&(mesg.mesg_type),mesg.mesg_len,0);
-
}
-
fclose(fp);
-
}
-
mesg.mesg_len=0;
-
msgsnd(msqid,&(mesg.mesg_type),mesg.mesg_len,0);
-
}
-
-
exit(0);
- }
error.c
-
#include "myinclude.h" /* for definition of errno */
-
#include<stdarg.h> /* ISO C variable aruments */
-
#include<stdlib.h>
-
#include<stdio.h>
-
#include<string.h>
-
#include<errno.h>
-
-
#define MAXLINE 4096
-
-
static void err_doit(int, int, const char *, va_list);
-
-
/*
-
* Nonfatal error related to a system call.
-
* Print a message and return.
-
*/
-
void
-
err_ret(const char *fmt, ...)
-
{
-
va_list ap;
-
-
va_start(ap, fmt);
-
err_doit(1, errno, fmt, ap);
-
va_end(ap);
-
}
-
-
/*
-
* Fatal error related to a system call.
-
* Print a message and terminate.
-
*/
-
void
-
err_sys(const char *fmt, ...)
-
{
-
va_list ap;
-
-
va_start(ap, fmt);
-
err_doit(1, errno, fmt, ap);
-
va_end(ap);
-
exit(1);
-
}
-
-
/*
-
* Fatal error unrelated to a system call.
-
* Error code passed as explict parameter.
-
* Print a message and terminate.
-
*/
-
void
-
err_exit(int error, const char *fmt, ...)
-
{
-
va_list ap;
-
-
va_start(ap, fmt);
-
err_doit(1, error, fmt, ap);
-
va_end(ap);
-
exit(1);
-
}
-
-
/*
-
* Fatal error related to a system call.
-
* Print a message, dump core, and terminate.
-
*/
-
void
-
err_dump(const char *fmt, ...)
-
{
-
va_list ap;
-
-
va_start(ap, fmt);
-
err_doit(1, errno, fmt, ap);
-
va_end(ap);
-
abort(); /* dump core and terminate */
-
exit(1); /* shouldn't get here */
-
}
-
-
/*
-
* Nonfatal error unrelated to a system call.
-
* Print a message and return.
-
*/
-
void
-
err_msg(const char *fmt, ...)
-
{
-
va_list ap;
-
-
va_start(ap, fmt);
-
err_doit(0, 0, fmt, ap);
-
va_end(ap);
-
}
-
-
/*
-
* Fatal error unrelated to a system call.
-
* Print a message and terminate.
-
*/
-
void
-
err_quit(const char *fmt, ...)
-
{
-
va_list ap;
-
-
va_start(ap, fmt);
-
err_doit(0, 0, fmt, ap);
-
va_end(ap);
-
exit(1);
-
}
-
-
/*
-
* Print a message and return to caller.
-
* Caller specifies "errnoflag".
-
*/
-
static void
-
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
-
{
-
char buf[MAXLINE];
-
-
vsnprintf(buf, MAXLINE, fmt, ap);
-
if (errnoflag)
-
snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
-
strerror(error));
-
strcat(buf, "\n");
-
fflush(stdout); /* in case stdout and stderr are the same */
-
fputs(buf, stderr);
-
fflush(NULL); /* flushes all stdio output streams */
- }
myinclude.h
-
#ifndef MYINCLUDE_H
-
#define MYINCLUDE_H
-
#include<fcntl.h>
-
/*定义一些常量*/
-
#define MAXLINE 4096/*max line length*/
-
#define FILE_MODE (S_IRUSR |S_IWUSR |S_IRGRP |S_IROTH)/*文件权限*/
-
-
#define MQ_KEY 1234L
-
#define MAXMESGDATA 1024
-
#define SVMSG_MODE 0644
-
-
/*在出错处理函数error.c里的一些原型*/
-
void err_ret(const char *fmt, ...);
-
void err_sys(const char *fmt, ...);
-
void err_exit(int,const char *fmt, ...);
-
void err_dump(const char *fmt, ...);
-
void err_quit(const char *fmt, ...);
-
-
struct mymesg
-
{
-
long mesg_len; /* #bytes in mesg_data, can be 0 */
-
long mesg_type; /* message type, must be > 0 */
-
char mesg_data[MAXMESGDATA];
-
};
-
- #endif