中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 未整理篇
IPphone日志3
作者:yirui 时间:2003-02-11 11:09 出处:互联网 责编:chinaitpower
              摘要:IPphone日志3

今天把队列代码修改成可操作代码(这个代码是仿照<<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);
}
*****************************************
这样就完成了一个简单的消息泵.


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有