资源简介
东北大学 软件学院 linux 大作业 sk老师 85分

代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include “msg.h“
int main()
{
int server_sockfd;//服务器端套接字
int client_sockfd;//客户端套接字
int len;
struct sockaddr_in my_addr; //服务器网络地址结构体
struct sockaddr_in remote_addr; //客户端网络地址结构体
int sin_size;
char buf[BUFSIZ]; //数据传送的缓冲区
memset(&my_addr0sizeof(my_addr)); //数据初始化--清零
my_addr.sin_family=AF_INET; //设置为IP通信
my_addr.sin_addr.s_addr=INADDR_ANY;//服务器IP地址
my_addr.sin_port=htons(8000); //服务器端口号
key_t key;
pid_t pid;
int msgid;
int TYPE_ME; //由getpid()来指定唯一该进程才有的mtype
struct msgbuf msg;
TYPE_ME = getpid(); //用自己的进程号作为自己接收消息的消息类型
/*
char recv[10];
int i = 0;
int gamenum;
*/
//生成消息队列的key
if((key = ftok(“./“‘s‘))==-1){
perror(“ftok error.“);
exit(0);
}
//如果服务器已创建消息队列才可以打开,客户端无权创建
if((msgid = msgget(keyMSG_Q_PERM))==-1){
printf(“[server maybe not on line...]\n“);
exit(1);
}
//实现登录操作
printf(“请输入登录用户名:“);
fgets(msg.user_nameMSG_SIZEstdin);
msg.user_name[strlen(msg.user_name)-1]=‘\0‘;
msg.mtype = MSG_TO_SERVER;
msg.subtype = 1;
msg.pid = getpid();
msgsnd(msgid&msgMSG_LEN0); //MSG_SIZE是消息正文大小
//客户端双进程实现读写操作
if((pid = fork())==-1){
perror(“fork error.“);
exit(1);
}else if(pid == 0){ //子进程负责读
while(1){
msgrcv(msgid&msgMSG_LENTYPE_ME0);
//判断接收到的消息是否为quit
if(strncmp(msg.mtext“quit“4) == 0){
printf(“[server will close in 3 seconds...]\n“);
kill(TYPE_MESIGUSR1); //若接收到服务端关闭,kill所有进程
exit(0);
}
//检测用户是否输入exit退出操作
if(strncmp(msg.mtext“exit“4) == 0){
msg.subtype = 3;
}
//写成switch方便功能扩展
switch(msg.subtype){
case 1:
printf(“[%s Login]\n“msg.user_name);
break;
case 2:
printf(“[%s]: %s“msg.user_namemsg.mtext);
break;
case 3:
printf(“[%s Logout]“msg.user_name);
break;
default :
break;
}
}
}else{ //父进程用于发送消息
msg.mtype = MSG_TO_SERVER;
msg.subtype = 2; //设为广播模式
while(1){
printf(“[%s]:\n“msg.user_name);
fgets(msg.mtextMSG_SIZEstdin);
msgsnd(msgid&msgMSG_LEN0);
//当此进程输入exit时,表示进程logout
if(strncmp(msg.mtext“exit“4)==0){
msg.subtype = 3;
msgsnd(msgid&msgMSG_LEN0);
sleep(2);
kill(pidSIGKILL);
exit(0);
}
/*
printf(“游戏开始“);
int i;
int a[100];
srand( time(NULL) ); //生成种子
for(i=0;i<100;i++)
{
a[i]=rand()%7; //生成一个1~6的随机数
}
msgsnd(msgid&msgMSG_LEN0); //发送骰子得数
*/
}
}
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3286 2017-12-27 22:21 client.c
文件 299 2017-12-26 20:58 msg.h
文件 5800 2017-12-27 22:31 server.c
----------- --------- ---------- ----- ----
9385 3
- 上一篇:基于BP神经网络模型的森林空气质量评价
- 下一篇:最新安徽县级shp文件
相关资源
- uboot到linux logo显示不间断 补丁
- UNIX/LINUX编程实践教程的源码
- Linux任务管理器
- linux应用层的华容道游戏源代码
- ubuntu9.10 可加载内核模块和字符设备驱
- MP3文件ID3v2ID3v2APEv2标签读取
- 操作系统实验——虚存管理实验
- linux下的发包工具sendip
- 尚观培训linux许巍关于c 的笔记和讲义
- 尚观培训linux董亮老师关于数据结构的
- linux 线程池源码 c 版
- linux C 电梯程序练习
- linux下用多进程同步方法解决生产者
- Linux 操作系统实验(全)
- Linux From Scratch 中文手册
- linux 网络实验 ftp程序
- Linux命令大全离线版&在线版
- 操作系统共享内存实验
- dos 下运行Linux 命令--gnu_utils
- linux 0.12内核源代码
- linux简易shell C实现
- linux实验报告及心得体会
- 基于GTK的Linux环境下的简易任务管理器
- linux扫雷游戏代码
- CAN Linux驱动代码
- Linux系统教材
- intel 82579LM 网卡驱动Linux系统版 v1.9.
- SA1110处理器掌上电脑液晶显示器设计
- 基于Linux的串口服务器设计
- Windows下访问LINUX的利器-SSH
评论
共有 条评论