📄 protocol.cpp
字号:
// protocol.cpp: implementation of the Cprotocol class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "server.h"
#include "protocol.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Cprotocol::Cprotocol()
{
}
Cprotocol::~Cprotocol()
{
}
//检查效验和,返回检查的结果
bool Cprotocol::examine_checksum(frame r)
{
int num;
int checkbit;
num=1;
//对包中的数据信息先进行奇偶校验,
while (r.info.data[num]!='\0')
{
r.info.data[num]=(r.info.data[num])^(r.info.data[num-1]);
num++;
}
//得出包中数据信息的校验位
checkbit=r.info.data[num-1];
//再把帧的类型和序号与校验位异或,最后得出这个帧的一个校验位
checkbit=checkbit^(r.kind);
checkbit=checkbit^(r.seq);
printf("the checkbit is %d\n",checkbit);
//查看校验结果是否正确
if (checkbit==r.checksum)
return true;
else
return false;
}//end of examine_checksum
//从物理层取到一个帧
int Cprotocol::from_physical_layer(SOCKET sock,frame &r)
{
int num;
num=recv(sock,(char *)&r,sizeof(r),0);
return num;
}// end of from_physical_layer
//将数据发送到网络层,从接收帧中取到信息并且发送到网络层
CString Cprotocol::to_network_layer(packet p)
{
CString tempstr;
tempstr.Format("%s",p.data );
return tempstr;
}
//end of to_network_layer
//将数据帧发往物理层准备传送
void Cprotocol::to_physical_layer(SOCKET sock,frame &s)
{
send(sock,(char*)&s,sizeof(s),0);
}
//end of to_physical_layer
//等待一个事件发生,返回事件类型
void Cprotocol::wait_for_event(SOCKET sock,event_type &event)
{
fd_set readfds;
struct timeval tv;
FD_ZERO(&readfds);//清空fdset与所有文件句柄的联系。
FD_SET(sock,&readfds);//建立文件句柄clisock与fdR的联系。
tv.tv_sec=0;
tv.tv_usec=0;
select(sock+1, &readfds, NULL, NULL,NULL);
if (FD_ISSET(sock, &readfds))
event=frame_arrival;
else
event=time_out;
}//end of wait_for_event
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -