91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

東北大學 軟件學院 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


評論

共有 條評論