From: Tufan BIYIKTAS (tufan.biyiktas@gmail.com)
Date: Fri 03 Jun 2005 - 04:03:09 EEST
msq_defs.h
------------
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#define SEED 'x'
#define PATH "."
#define BUFSIZE 4096
typedef struct {
long int msg_to;
long int msg_fm;
char buffer[BUFSIZE];
}message_t;
const long int SERVER=1L;
const long int CLIENT=2L;
server.c
--------
#include "msq_defs.h"
extern const long int SERVER;
extern const long int CLIENT;
int main()
{
key_t key;
int msq_id;
message_t msg;
int len = 2*sizeof(long int)+BUFSIZE*sizeof(char);
// get message key
key = ftok(PATH,SEED);
if(key == -1){
printf("\nftok error !!!\n");
exit(1);
}
printf("msq id ... : %d\n");
// creat a message queue
if( (msq_id = msgget(key,IPC_CREAT | 0660)) < 0 )
{
printf("\nmsgget error !!!\n");
exit(2);
}
while(1){
memset(msg.buffer,0x0,BUFSIZE);
msgrcv(msq_id,&msg,len,CLIENT,0);
printf("\nmsg msg_fm ...%s\n",msg.msg_fm);
printf("\nmsg msg_to ...%s\n",msg.msg_to);
printf("\nmsg buffer ...%s\n",msg.buffer);
}
msgctl(msq_id, IPC_RMID, (struct msqid_ds *) 0);
return 0;
}
client.c
-------
#include "msq_defs.h"
extern const long int SERVER;
extern const long int CLIENT;
int main()
{
key_t key;
int msq_id;
message_t msg;
int len = 2*sizeof(long int)+BUFSIZE*sizeof(char);
// get message key
key = ftok(PATH,SEED);
if(key == -1){
printf("\nftok error !!!\n");
exit(1);
}
printf("msq id ... : %d\n");
// creat a message queue
if( (msq_id = msgget(key,IPC_CREAT | 0660)) < 0 )
{
printf("\nmsgget error !!!\n");
exit(2);
}
memset(msg.buffer,0x0,BUFSIZE);
msg.msg_to=SERVER;
msg.msg_fm=CLIENT;
strcpy(msg.buffer,"qwerty123");
msgsnd(msq_id,&msg,len,0);
msgctl(msq_id, IPC_RMID, (struct msqid_ds *) 0);
return 0;
}
_______________________________________________
Linux-programlama mailing list
Linux-programlama@liste.linux.org.tr
http://liste.linux.org.tr/mailman/listinfo/linux-programlama