System V实现的一个消息回射服务器与客户端

echocli.c

#include <unistd.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

#include <string.h>

#define ERR_EXIT(m) \

do \

{ \

perror(m); \

exit(EXIT_FAILURE); \

} while(0)

#define MSGMAX 8192

struct msgbuf {

long mtype;       /* message type, must be > 0 */

char mtext[MSGMAX];    /* message data */

};

void echo_cli(int msgid)

{

int n;

int pid;

pid = getpid();

struct msgbuf msg;

memset(&msg, 0, sizeof(msg));

*((int*)msg.mtext) = pid;

while (fgets(msg.mtext+4, MSGMAX, stdin) != NULL)

{

msg.mtype = 1;

if (msgsnd(msgid, &msg, 4+strlen(msg.mtext+4), 0) < 0)

ERR_EXIT("msgsnd");

memset(msg.mtext+4, 0, MSGMAX-4);

if ((n = msgrcv(msgid, &msg, MSGMAX, pid, 0)) < 0)

ERR_EXIT("msgsnd");

fputs(msg.mtext+4, stdout);

memset(msg.mtext+4, 0, MSGMAX-4);

}

}

int main(int argc, char *argv[])

{

int msgid;

msgid = msgget(1234, 0);

if (msgid == -1)

ERR_EXIT("msgget");

echo_cli(msgid);

return 0;

}

echosrv.c

#include <unistd.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

#include <string.h>

#define ERR_EXIT(m) \

do \

{ \

perror(m); \

exit(EXIT_FAILURE); \

} while(0)

#define MSGMAX 8192

struct msgbuf {

long mtype;       /* message type, must be > 0 */

char mtext[MSGMAX];    /* message data */

};

void echo_srv(int msgid)

{

int n;

struct msgbuf msg;

memset(&msg, 0, sizeof(msg));

while (1)

{

if ((n = msgrcv(msgid, &msg, MSGMAX, 1, 0)) < 0)

ERR_EXIT("msgsnd");

int pid;

pid = *((int*)msg.mtext);

fputs(msg.mtext+4, stdout);

msg.mtype = pid;

msgsnd(msgid, &msg, n, 0);

}

}

int main(int argc, char *argv[])

{

int msgid;

msgid = msgget(1234, IPC_CREAT | 0666);

if (msgid == -1)

ERR_EXIT("msgget");

echo_srv(msgid);

return 0;

}

makefile:

.PHONY:clean all

CC=gcc

CFLAGS=-Wall -g

BIN=echosrv echocli

all:$(BIN)

%.o:%.c

$(CC) $(CFLAGS) -c $< -o [email protected]

clean:

rm -f *.o $(BIN)

System V实现的一个消息回射服务器与客户端

时间: 2024-08-06 02:26:44

System V实现的一个消息回射服务器与客户端的相关文章

C语言实现最基本的回射服务器与客户端(服务器用TCP协议回射客户发来的消息)

话不多说,直接上干货,下面两个程序都是linux程序. server.c完整代码: #include <stdio.h>#include <string.h>#include <unistd.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>

回射服务器的客户端

#include "unp.h"int main(int argc,char**argv){ int sockfd; struct sockaddr_in servaddr; if(argc != 2) err_quit("usage:tcpcli<IPaddress>"); sockfd = socket(AF_INET,SOCK_STREAM,0); //SOCK_STRREAM为TCP协议,通信协议类型 bzero(&servaddr,si

TCP回射服务器/客户端分析

本文将对一个简单的TCP回射服务器和客户端进行抓包,从而分析一次成功而理想TCP会话的基本流程,多次不成功或与预期不一致的抓包结果将在下篇博文进行分析 本文程序编译环境为: Linux version 3.16.4-1-ARCH gcc version 4.9.1 20140903 (prerelease) Glibc 2.18 服务器代码如下: 1 #include <unistd.h> 2 #include <sys/types.h> 3 #include <sys/so

Linux程序设计学习笔记----System V进程通信之消息队列

一个或多个进程可向消息队列写入消息,而一个或多个进程可从消息队列中读取消息,这种进程间通讯机制通常使用在客户/服务器模型中,客户向服务器发送请求消息,服务器读取消息并执行相应请求.在许多微内核结构的操作系统中,内核和各组件之间的基本通讯方式就是消息队列.例如,在 MINIX 操作系统中,内核.I/O 任务.服务器进程和用户进程之间就是通过消息队列实现通讯的. Linux中的消息可以被描述成在内核地址空间的一个内部链表,每一个消息队列由一个IPC的标识号唯一的标识.Linux 为系统中所有的消息队

第九章 TCP和UDP同时用复用一个端口实现一个回射服务器

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include <string.h> #include

UNIX网络编程卷1 回射服务器程序 TCP服务器程序设计范式 四个版本

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 这是一个简单的回射服务器程序.它将客户发送的数据读入缓冲区并回射其中内容 下面我会介绍同一个使用 TCP 协议的回射服务器程序的几个不同版本,分别是 fork 版本.select 版本.poll 版本.多线程版本 fork 版本:为每一个客户连接派生(fork) 一个子进程用来处理客户请求 /** * TCP/IPv4 协议相关 * **/ #include "unp.h" in

第十二篇:并发回射服务器的具体实现及其中僵尸子进程的清理( 上 )

前言 本文将分为两个部分,第一部分具体实现一对并发回射服务器/客户程序( 看过前面那篇文章的这部分可不看 重复了 ):第二部分为服务器添加僵尸子进程自动清理机制. 那么服务器具体怎么实现并发?怎么会有僵尸进程?僵尸进程又是什么?如何处理这些僵尸进程 ... 本文将为你一一解惑. 回射并发服务器 功能:接收用户发送过来的数据后再发送回用户,且能同时处理多个用户请求. 大体思路:每当收到用户请求,服务器就fork一个子进程,让子进程去处理客户请求. 实现代码: 1 #include "unp.h&q

socket编程之并发回射服务器3

在socket编程之并发回射服务器一文中,服务器采用多进程的方式实现并发,本文采用多线程的方式实现并发. 多线程相关API: // Compile and link with -pthread int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); int pthread_join(pthread_t thread, void **

TCP回射服务器修订版(ubuntu 18.04)

一.需求 把https://www.cnblogs.com/soldierback/p/10673345.html中的TCP回射服务器程序重写成使用select来处理任意个客户的单进程 程序,而不是为每个进程派生一个子进程 二.分析 (1)服务器有单个监听描述符    (2)服务器只维护一个读描述符集:假设服务器是在前台启动的,那么描述符0.1.2将分别被设置为标准输入.标准输出和标准错误输出:可见监听   套接字的第一个可用描述符是3    (3)服务器维护一个名为clients的整型数组,它