server.c — ждет сообщения, в ответ посылает своё.
#include <sys/types.h> // ftok()
#include <sys/ipc.h>
#include <sys/msg.h> // msgget()
#include <string.h> // memset()
#include <stdio.h> // printf()
#include "message.h"
int hQueue; // for finish()
int main () {
key_t hKey = ftok("test-server",'A');
if (hKey < 0) {
printf("Cannot create IPC-key. Please, create empty file named 'test-server'.\n");
}
hQueue = msgget(hKey, 0666 | IPC_CREAT);
if (hQueue < 0) {
printf("Cannot create message queue.\n");
}
int size;
TMessage messageRecv;
messageRecv.mtype = 1L;
size = msgrcv (hQueue, &messageRecv, sizeof(TMessage), messageRecv.mtype, 0);
if (size < 0) { printf("Cannot receive message from queue.\n"); }
else { printf("Received. Result: %d\n",size); }
int i;
for (i = 0; i < sizeof(TMessage); i++) {
printf("%d: %02X\n",i,((unsigned char *)&messageRecv)[i]);
}
TMessage messageSend;
memset(&messageSend,0x00,sizeof(TMessage));
messageSend.mtype = 1L;
messageSend.size = 2L;
size = msgsnd (hQueue, &messageSend, sizeof(TMessage), 0);
if (size < 0) { printf("Cannot send message to queue.\n"); }
else { printf("Send. Result: %d\n",size); }
if (msgctl(hQueue,IPC_RMID,0) < 0) {
printf("Cannot remove message queue.\n");
}
else {
printf("Message queue removed.\n");
}
return 0;
}
#include <sys/types.h> // ftok()
#include <sys/ipc.h>
#include <sys/msg.h> // msgget()
#include <string.h> // memset()
#include <stdio.h> // printf()
#include "message.h"
int main() {
printf("Size of TMessage: %lu\n",sizeof(TMessage));
printf("Size of long: %lu\n",sizeof(long));
printf("---\n");
key_t hKey = ftok("test-server",'A');
if (hKey < 0) {
printf("Cannot create IPC-key. Please, create empty file named 'test-server'.\n");
}
int hQueue = msgget(hKey, 0);
if (hQueue < 0) {
printf("Cannot attach to message queue.\n");
}
int size;
TMessage messageSend;
memset(&messageSend,0x00,sizeof(TMessage));
messageSend.mtype = 1L;
size = msgsnd(hQueue,&messageSend,sizeof(TMessage),0);
if (size < 0) { printf("Cannot send message to queue.\n"); }
else { printf("Send. Result: %d.\n",size); }
TMessage messageRecv;
memset(&messageRecv,0x00,sizeof(TMessage));
messageRecv.mtype = 0L;
size = msgrcv(hQueue,&messageSend,sizeof(TMessage),messageRecv.mtype,0);
if (size < 0) { printf("Cannot receive message from queue.\n"); }
else { printf("Received. Result: %d\n",size); }
printf("Size: %lu\n",messageRecv.size);
int i;
for (i = 0; i < sizeof(TMessage); i++) {
printf("%d: %02X\n",i,((unsigned char *)&messageRecv)[i]);
}
return 0;
}
#ifndef __MESSAGE_H__
#define __MESSAGE_H__
// Для упрощения - минимальные значения
#define MD5_SIZE 1
#define MAX_DATA_SIZE 1
typedef struct _TMessage {
long mtype;
unsigned char session_id[MD5_SIZE+1];
long size;
char buf[MAX_DATA_SIZE];
} TMessage;
#endif
Вопрос в том, что сообщение, получаемое клиентом - «пустое»:
$ ./server
Received. Result: 32
0: 01
1: 00
2: 00
3: 00
4: 00
5: 00
6: 00
7: 00
8: 00
9: 00
10: 00
11: 00
12: 00
13: 00
14: 00
15: 00
16: 00
17: 00
18: 00
19: 00
20: 00
21: 00
22: 00
23: 00
24: 00
25: 00
26: 00
27: 00
28: 00
29: 00
30: 00
31: 00
Send. Result: 0
Message queue removed.
$ ./client
Size of TMessage: 32
Size of long: 8
---
Send. Result: 0.
Received. Result: 32
Size: 0
0: 00
1: 00
2: 00
3: 00
4: 00
5: 00
6: 00
7: 00
8: 00
9: 00
10: 00
11: 00
12: 00
13: 00
14: 00
15: 00
16: 00
17: 00
18: 00
19: 00
20: 00
21: 00
22: 00
23: 00
24: 00
25: 00
26: 00
27: 00
28: 00
29: 00
30: 00
31: 00