From: Tufan BIYIKTAS (tufan.biyiktas@gmail.com)
Date: Fri 03 Jun 2005 - 14:46:52 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; // kim gonderiyor
long int msg_fm; // kime gonderiyor
char buffer[BUFSIZE]; // gonderilen data
}message_t;
const long int SERVER=1L; // server icin id
const long int CLIENT=2L; // client icin id
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);
}
// creat a message queue
if( (msq_id = msgget(key,IPC_CREAT | 0660)) < 0 )
{
printf("\nmsgget error !!!\n");
exit(2);
}
//while(1){
// buffer i temizle
memset(msg.buffer,0x0,BUFSIZE);
// queue dan CLIENT in yazdigini oku
msgrcv(msq_id,&msg,len,CLIENT,0);
// msg nin icerigini yaz
printf("\nmsg msg_fm ... : %ld\n",msg.msg_fm);
printf("\nmsg msg_to ... : %ld\n",msg.msg_to);
printf("\nmsg buffer ... : %s\n" ,msg.buffer);
//}
// kuyrugu sil
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);
}
// creat a message queue
if( (msq_id = msgget(key,IPC_CREAT | 0660)) < 0 )
{
printf("\nmsgget error !!!\n");
exit(2);
}
// buffer i icerigini temizle
memset(msg.buffer,0x0,BUFSIZE);
// CLIENT --> SERVER a mesg
msg.msg_to=SERVER;
msg.msg_fm=CLIENT;
strcpy(msg.buffer,"qwerty123");
// msg yi gonder
msgsnd(msq_id,&msg,strlen(msg.buffer),0);
// queue yi temizle
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