视频笔记3

#pragma once是一个比较常用的C/C++预处理指令,只要在头文件的最开始加入这条预处理指令,就能够保证头文件只被编译一次。
宏定义的优点:
方便程序的修改
提高运行效率
日志宏:
#include <stdio.h>
#define MacroLog(...)
{
FILE* file;
fopen_s(&file,"./a.txt","a");
if(file !=nullptr)
{
 fprintf(file,"%s:Line %d",__FILE__,__LINE__);
 fprintf(file,__VA_ARGS__);
 fprintf(file," ");
}
fclose(file);
}

共享内存:
//  16进制 大小 标志:IPC_CREAT IPC_EXCL  返回一个描述符  
int shmget(key_t key,size_t size, int shmflg)
void *

int main()
{
 int shmid = -1;
 //获得共享内存  IPC_EXCL  如果不存在 则创建  存在则报错
 shmid = shmget(0x1111,256,0664|IPC_CREATE|IPC_EXCL);
 if (shmid !=-1)
 {
  perror("error");
 }

}
ipcs 查看共享内存
 cleanipc test04 all
// 返回共享内存的首地址    推荐的首地址   读写权限你 
 void *shmat(int shmid, const void* shmaddr, int shmflg)
buf = shmat(shmid,NULL,SHM_RND)  //读写
if(buf == -1)
{
 
 return -1;
}
memcopy(buf,"hello",5);
printf("%s",buf);
nattch : 代表有一个用户在使用共享内存 

void shmdt(void *buf)  取消与共享内存关联
unlink 连接计数

int ret = shmclt(shmid,SHM_RMID,NULL)  //当没有用户使用时删除
if (ret == -1)
{
 printf("error")
}
多用户共享内存:
//  映射函数   void**  传出参数  建立关系
int IPC_Mapshm(int shmhdl, void** mapaddr)

 void *tempptr = NULL;  // 文件描述符  读写权限
 tempptr = (void*)shmat(shmhdl,  0, SHM_RND);
 if((int)tempptr == -1)
  return -1;
  *mapaddr = tmeppter;
  return ;

}
删除共享
shmclt(shmid,SHM_RMID,NULL)
密钥协商

typedef MsgKey_Req:   编码发送--》  解码--》放入共享内存
 cmdType
 client
 AuthCode
 serverId
 r1
typedef MsgKey_res:    编码发送--》  解码--》放入共享内存
 rv
 clientId
 serverId
 r2
 seckey
 
连接池: 通信的数据量
用文件描述符通信
pthread_create(&pid,NULL,mystart,(void*) conned);创建线程
for(int i  = 0;i<ithreadNum; i++)
{
 INfo *info= (Info*)malloc(sizeof(Info));
 info.handle = handle  ;  连接池句柄  共享
 info.loopNUM = loopnum;  私有
 info.index = i;   私有
}
服务器结构体
服务器初始化
线程函数:
MsgDecode()
 
 
原文地址:https://www.cnblogs.com/countryboy666/p/11516625.html