利用System V消息队列实现回射客户/服务器
一、介绍
在学习UNIX网络编程 卷1时,我们当时可以利用Socket套接字来实现回射客户/服务器程序,但是Socket编程是存在一些不足的,例如:
1. 服务器必须启动之时,客户端才能连上服务端,并与服务端进行通信;
2. 利用套接口描述符进行通信,必须知道对端的IP与端口。
二、相关函数介绍
下面,我们利用System V消息队列来实现进程间的通信:
首先,我们先来了解一下下面几个函数:
1. msgget: 该函数用于打开或创建消息队列,其作用相当与文件操作函数open。
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h> int msgget(key_t key, int msgflg);
2. msgsnd:消息发送函数
3. msgrcv:消息接收函数
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h> int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,
int msgflg);
4. struct msgbuf
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[]; /* message data */
};
mtype 消息类型
mtext 消息内容
三、实现原理
服务器端:只接收类型为1的消息,接收完毕之后,取出mtext的前四个字节并存储为client_pid,并将消息类型mtype修改为client_pid;
客户端:只发送类型为1的消息,即将 mtype 置为1,并将mtext的前四个字节的内容设为自身的进程id,即pid.
四、编程实现如下:
客户端:
/*************************************************************************
> File Name: echocli.c
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: Tue 28 Oct 2014 11:25:48 AM HKT
************************************************************************/ #include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h> #define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while()
#define MSGMAX 8192 struct msgbuf
{ long mtype; /* type of message */
char mtext[MSGMAX]; /* message text */
}; void echo_cli(int msgid)
{
int pid = getpid();
int n; struct msgbuf msg;
memset(&msg, , sizeof(msg));
msg.mtype = ;
*((int*)msg.mtext) = pid; while(fgets(msg.mtext + , MSGMAX, stdin) != NULL)
{
msg.mtype = ;
if(msgsnd(msgid, &msg, + strlen(msg.mtext + ), IPC_NOWAIT) < )
ERR_EXIT("msgsnd");
memset(msg.mtext + , , MSGMAX - );
if((n = msgrcv(msgid, &msg, MSGMAX, pid, )) < )
ERR_EXIT("msgrcv");
fputs(msg.mtext + , stdout);
memset(msg.mtext + , , MSGMAX - );
}
} int main(int argc, char **argv)
{
int msgid;
msgid = msgget(, );//open
if(msgid == -)
ERR_EXIT("msgget"); echo_cli(msgid);
return ;
}
服务端:
/*************************************************************************
> File Name: echosrv.c
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: Tue 28 Oct 2014 11:25:58 AM HKT
************************************************************************/ #include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h> #define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while() #define MSGMAX 8192 struct msgbuf
{
long mtype; /* type of message */
char mtext[MSGMAX]; /* message text */
}; void echo_srv(int msgid)
{
int n;
struct msgbuf msg;
memset(&msg, , sizeof(msg));
while()
{
//only recv the message of type = 1
if((n = msgrcv(msgid, &msg, MSGMAX, , )) < )
{
ERR_EXIT("msgrcv");
} fputs(msg.mtext + , stdout);
//client pid
int pid;
pid = *((int*)msg.mtext); //回射
msg.mtype = pid;//type
msgsnd(msgid, &msg, n, );
memset(&msg, , sizeof(msg));
}
} int main(int argc, char **argv)
{
int msgid;
msgid = msgget(, IPC_CREAT | );
if(msgid == -)
{
ERR_EXIT("msgget");
}
echo_srv(msgid);
return ;
}
存在问题:
1. 不同主机间不同的进程无法进行通信。
2. 消息的长度受到限制MSGMAX
3. 消息的数量收到限制MSGMNB
4. 会出现死锁问题。
利用System V消息队列实现回射客户/服务器的更多相关文章
- 用system v消息队列实现回射客户/服务器程序
客户端程序 #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include< ...
- 消息队列实现回射客户/服务器和 msgsnd、msgrcv 函数
一.msgsnd 和 msgrcv 函数 #include <sys/types.h> #include <sys/ipc.h> #include <sys/ms ...
- Linux IPC实践(6) --System V消息队列(3)
消息队列综合案例 消息队列实现回射客户/服务器 server进程接收时, 指定msgtyp为0, 从队首不断接收消息 server进程发送时, 将mtype指定为接收到的client进程的pid ...
- 第二十七章 system v消息队列(三)
消息队列实现回射客户/服务器 msg_srv.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> ...
- 进程间通信 System V 消息队列
1.msgget (key_t ket,int flag) ; //创建一个新的消息队列或者访问一个已存在的消息队列 2.msgsnd(int msid, const void *ptr ,size_ ...
- 第6章 System V消息队列
6.1 概述 System V消息队列在内核中是list存放的,头结点中有2个指针msg_first 和msg_last.其中每个节点包含:下个节点地址的指针.类型.长度.数据等. 6.2 函数 6. ...
- Linux进程通信之System V消息队列
System V消息队列是Open Group定义的XSI,不属于POSIX标准.System V IPC的历史相对很早,在上个世70年代后期有贝尔实验室的分支机构开发,80年代加入System V的 ...
- UNIX环境高级编程——system V消息队列
unix早期通信机制中的信号能够传送的信息量有限,管道则只能传送无格式字节流,这远远是不够的. 消息队列(也叫报文队列)客服了这些缺点: 消息队列就是一个消息的链表. 可以把消 ...
- linux c编程:System V消息队列一
消息队列可以认为是一个消息链表,System V 消息队列使用消息队列标识符标识.具有足 够特权的任何进程都可以往一个队列放置一个消息,具有足够特权的任何进程都可以从一个给定队列读出一个消息.在某个进 ...
随机推荐
- Silverlight开发工具汇总
随着Silverlight技术的逐步完善,Silverlight应用大批的涌现,近期的2010年冬季奥运会,Silverlight作为首选视频播放技术,为全球提供在线赛事实况. Silverlight ...
- DOM对象控制HTML无素——详解1
getElementsByName()方法 返回带有指定名称的节点对象的集合. 语法: document.getElementsByName(name) 与getElementById() 方法不同的 ...
- Python 字典 Print 格式化
__author__ = 'dell' ab = {'Swaroop': 'swaroopch@byteofpython.info', 'Larry': 'larry@wall.org', 'Mats ...
- Java中swap解惑
直接上代码…… public class Swap { public static void main(String[] args) { int a[] = new int[]{1,2}; Syste ...
- Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants?
Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants? Introduction ...
- 牢记负载均衡与HA,高性能是不同的方案。一般的CLUSTER只能实现其中的一种,而ORACLE的RAC可以有两种。
F5/LVS<—Haproxy<—Squid/Varnish<—AppServer. 现在网站发展的趋势对网络负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术: 第一 ...
- KeilC51使用详解 (一)
第一节 系统概述 Keil C51是美国Keil Software公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性.可维护性上有明显的优势,因而易学易用.用过 ...
- nodejs触发事件的两种方式
nodejs触发事件的两种方式: 方式之一:通过实例化events.EventEmitter //引入events模块 var events = require('events'); //初始化eve ...
- WordPress Pie Register插件‘wp-login.php’多个跨站脚本漏洞
漏洞名称: WordPress Pie Register插件‘wp-login.php’多个跨站脚本漏洞 CNNVD编号: CNNVD-201307-255 发布时间: 2013-07-31 更新时间 ...
- Linux Shell编程(8)——变量详解
不同与许多其他的编程语言,Bash不以"类型"来区分变量.本质上来说,Bash变量是字符串,但是根据环境的不同,Bash允许变量有整数计算和比较.其中的决定因素是变量的值是不是只含 ...