資源簡介
使用消息隊列與共享內存完成一個簡單的終端聊天程序

代碼片段和文件信息
#include?“public.h“
void?*login_ptr?=?NULL;
void?signal_login(int?signo)
{
int?i?=?0;
printf(“new?user?login\n“);
char?login_head[100]?=?{0};
memcpy(login_headlogin_ptr100);
printf(“current?online?users:\n“);
for?(i?=?0;i?100;i++)
{
//表示被使用,有數據
if?(login_head[i]?==?1)
{
int?pos?=?100?+?sizeof(int)?*?i;
int?pid?=?0;
? memcpy(&pid(char*)login_ptr?+?possizeof(int));
? printf(“?%d?“pid);
}
}
? printf(“\n“);
}
int?main()
{
signal(SIGUSR1signal_login);
//登錄消息
//創建一個登錄消息隊列
int?msg_login_id?=?msgget((key_t)0001IPC_CREAT?|?0766);
if?(msg_login_id?0)
{
printf(“create?msg?error!“);
return?0;
}
//打開在線用戶共享內存
int?shmid?=?shmget((key_t)123400);
if?(shmid?0)
{
printf(“open?shm?error!“);
}
//映射邏輯地址
login_ptr?=?shmat(shmidNULL0);
LOGIN_T?login;
memset(&login0sizeof(LOGIN_T));
login.type?=?1;//表示登錄
login.pid??=?getpid();
//發送登錄數據
int?ret?=?msgsnd(msg_login_id(void*)&loginsizeof(LOGIN_T)-sizeof(long)0);
if?(ret?0)
{
printf(“send?login?msg?error!“);
return?0;
}
while?(1)
{
sleep(1);
}
return?0;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-08-05?00:10??IPC_chat\
?????目錄???????????0??2016-08-05?10:29??IPC_chat\IPC_chat\
?????文件?????1567744??2016-08-05?10:29??IPC_chat\IPC_chat.ncb
?????文件?????????890??2016-08-05?00:10??IPC_chat\IPC_chat.sln
?????文件???????11264??2016-08-05?10:29??IPC_chat\IPC_chat.suo
?????文件????????1202??2016-08-05?10:29??IPC_chat\IPC_chat\client.cpp
?????文件????????4042??2016-08-05?08:56??IPC_chat\IPC_chat\IPC_chat.vcproj
?????文件????????1415??2016-08-05?10:29??IPC_chat\IPC_chat\IPC_chat.vcproj.lenovo-PC.lenovo.user
?????文件?????????385??2016-08-05?08:58??IPC_chat\IPC_chat\public.h
?????文件???????29016??2016-08-05?10:25??IPC_chat\IPC_chat\server
?????文件????????1520??2016-08-05?10:29??IPC_chat\IPC_chat\server.cpp
評論
共有 條評論