今天把队列代码修改成可操作代码(这个代码是仿照<<linux程序设计>>修改的)
*********************************************CommMain.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "Msg_queue.h"
int save_errno;
static int server_running = 1;
static void process_command(const sSIPMsg mess_command);
void catch_signals()
{
server_running = 0;
}
int CommMain(int argc, char *argv[]) {
sSIPMsg mess_command;
if (!server_starting()) exit(EXIT_FAILURE);
while(server_running) {
if (read_request_from_client(&mess_command)) {
process_command(mess_command);
} else {
if(server_running) fprintf(stderr, "Server ended - can not \
read pipe\n");
server_running = 0;
}
} /* while */
server_ending();
exit(EXIT_SUCCESS);
}
/*
Any client messages are fed to the process_command function, where they are fed into a case statement that makes the appropriate calls to cd_dbm.c. */
static void process_command(const sSIPMsg comm)
{
sSIPMsg resp;
int first_time = 1;
resp = comm; /* copy command back, then change resp as required */
save_errno = 0;
switch(resp.request) {
case sip_Invite:
break;
case sip_Ack:
break;
case sip_Cancel:
break;
case sip_Options:
break;
case sip_Bye:
break;
case sip_Register:
break;
default:
break;
} /* switch */
if (!send_resp_to_client(resp)) {
fprintf(stderr, "Server Warning:-\
failed to respond to %d\n", resp.ModuleID);
}
return;
}
**********************"Msg_queue.h"
#ifndef _BASE_DEFINE_H
#define _BASE_DEFINE_H
typedef enum {
sip_Invite = 0,
sip_Ack,
sip_Cancel,
sip_Options,
sip_Bye,
sip_Register,
} client_request_e;
typedef struct {
//pid_t client_pid;
long ModuleID;//module ID
client_request_e request;
char message[100];
}sSIPMsg;
typedefstruct {
long msg_key; // type of message
sSIPMsg SipMsg; //message text */
}sMsgBuf;
int server_starting(void);
void server_ending(void);
int read_request_from_client(sSIPMsg *rec_ptr);
int send_resp_to_client(const sSIPMsg mess_to_send);
#endif // _BASE_DEFINE_H
*******************************************************
Msg_queue.c
***********************************
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "msg_queue.h"
#define SERVER_MQUEUE 1234
#define CLIENT_MQUEUE 4321
/* Two variables with file scope hold the two queue identifiers returned from the
msgget function. */
static int serv_qid = -1;
static int cli_qid = -1;
/* We make the server responsible for creating both message queues. */
int server_starting()
{
#if DEBUG_TRACE
printf("%d :- server_starting()\n", getpid());
#endif
serv_qid = msgget((key_t)SERVER_MQUEUE, 0666 | IPC_CREAT);
if (serv_qid == -1) return(0);
cli_qid = msgget((key_t)CLIENT_MQUEUE, 0666 | IPC_CREAT);
if (cli_qid == -1) return(0);
return(1);
}
/* The server is also responsible for tidying up if it ever exits. When the server ends,
we set our file scope variables to illegal values. This will catch any bugs if the server
attempts to send messages after it has called server_ending. */
void server_ending()
{
#if DEBUG_TRACE
printf("%d :- server_ending()\n", getpid());
#endif
(void)msgctl(serv_qid, IPC_RMID, 0);
(void)msgctl(cli_qid, IPC_RMID, 0);
serv_qid = -1;
cli_qid = -1;
}
/* The server read function reads a message of any type (that is, from any client)
from the queue, and returns the data part (ignoring the type) of the message. */
int read_request_from_client(sSIPMsg *rec_ptr)
{
struct sMsgBuf my_msg;
#if DEBUG_TRACE
printf("%d :- read_request_from_client()\n", getpid());
#endif
if (msgrcv(serv_qid, (void *)&my_msg, sizeof(*rec_ptr), 0, 0) == -1) {
return(0);
}
*rec_ptr = my_msg.SipMsg;
return(1);
}
/* Sending a response uses the client process ID that was stored in the request to address
the message. */
int send_resp_to_client(const sSIPMsg mess_to_send)
{
struct sMsgBuf my_msg;
#if DEBUG_TRACE
printf("%d :- send_resp_to_client()\n", getpid());
#endif
my_msg.SipMsg = mess_to_send;
my_msg.msg_key = mess_to_send.ModuleID
if (msgsnd(cli_qid, (void *)&my_msg, sizeof(mess_to_send), 0) == -1) {
return(0);
}
return(1);
}
*****************************************
这样就完成了一个简单的消息泵.
本文地址:http://com.8s8s.com/it/it34361.htm