📄 usrskt.cpp
字号:
// usrSkt.cpp : implementation file
//
#include "stdafx.h"
#include "winCltAll.h"
#include "usrSkt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CWinCltAllApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CusrSkt
CusrSkt::CusrSkt()
{
stateRecv = STAT_RECV_BEGIN;
recvRemainLen = NET_CMD_HEADLEN;
recvedLen = 0;
stateSend = STAT_SEND_BEGIN;
sendRemainLen = 0;
sentLen = 0;
}
CusrSkt::~CusrSkt()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CusrSkt, CAsyncSocket)
//{{AFX_MSG_MAP(CusrSkt)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CusrSkt member functions
void CusrSkt::OnConnect(int nErrorCode)
{
BOOL nodelayflag = TRUE;
// TODO: Add your specialized code here and/or call the base class
if(nErrorCode == 0)
{
SetSockOpt(TCP_NODELAY, &nodelayflag, sizeof (BOOL));
AsyncSelect(FD_READ|FD_CLOSE);
MessageBox(NULL,"Connected","onConnect",MB_OK);
EnableWindow(GetDlgItem(theApp.m_pMainWnd->m_hWnd,ID_connectServer),FALSE);
EnableWindow(GetDlgItem(theApp.m_pMainWnd->m_hWnd,ID_cmdSendLong),TRUE);
EnableWindow(GetDlgItem(theApp.m_pMainWnd->m_hWnd,ID_cmdSendShort),TRUE);
}
else
{
Close();
{
MessageBox(NULL,"无法连接,请检查网络和IP与端口号的设置","onConnect",MB_OK);
EnableWindow(GetDlgItem(theApp.m_pMainWnd->m_hWnd,ID_connectServer),TRUE);
}
}
CAsyncSocket::OnConnect(nErrorCode);
}
void CusrSkt::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
EnableWindow(GetDlgItem(theApp.m_pMainWnd->m_hWnd,ID_cmdSendLong),FALSE);
EnableWindow(GetDlgItem(theApp.m_pMainWnd->m_hWnd,ID_cmdSendShort),FALSE);
EnableWindow(GetDlgItem(theApp.m_pMainWnd->m_hWnd,ID_connectServer),TRUE);
CAsyncSocket::OnClose(nErrorCode);
}
void CusrSkt::OnReceive(int nErrorCode)
{
char buff[20];
unsigned int cmdNum;
int len;
int recvLen;
// TODO: Add your specialized code here and/or call the base class
if(nErrorCode==0)
{
if(stateRecv == STAT_RECV_BEGIN)
{
recvRemainLen = NET_CMD_HEADLEN;
recvedLen = 0;
stateRecv = STAT_RECV_HEAD;
}
if(recvRemainLen+recvedLen<=NET_MSG_MAXLEN)
{
recvLen = Receive(recvBuff+recvedLen,recvRemainLen,0);
}
else
{
recvLen = Receive(recvBuff+recvedLen,NET_MSG_MAXLEN-recvedLen,0);
}
if((recvLen!=SOCKET_ERROR) && (recvLen!=0))
{
recvRemainLen = recvRemainLen-recvLen;
recvedLen = recvedLen + recvLen;
if(recvRemainLen==0)
{
/*头部接收完毕, 显示命令号、命令长度*/
if(stateRecv == STAT_RECV_HEAD)
{
cmdNum = ((recvBuff[0]<<8)&0xFF00)+recvBuff[1];
len = ((recvBuff[2]<<24)&0xFF000000)+((recvBuff[3]<<16)&0xFF0000)
+((recvBuff[4]<<8)<<0xFF00)+recvBuff[5];
sprintf(buff,"0x%04x",cmdNum);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdNumReturn),buff);
sprintf(buff,"%d",len);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdLenReturn),buff);
recvRemainLen = len-NET_CMD_HEADLEN;
recvedLen = 0;
if(recvRemainLen==0)
{
stateRecv = STAT_RECV_BEGIN;
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam1Return),DFT_CMD_RETURN);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam2Return),DFT_CMD_RETURN);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam3Return),DFT_CMD_RETURN);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam4Return),DFT_CMD_RETURN);
}
else
{
stateRecv = STAT_RECV_MIDDLE;
}
}
else if(stateRecv == STAT_RECV_REAR)
{
stateRecv = STAT_RECV_BEGIN;
}
}
if((stateRecv==STAT_RECV_MIDDLE)&&
((recvedLen>=4)||(recvRemainLen==0)))
{
/*参数接收完毕*/
if((recvedLen>=4)&&(recvRemainLen>0))
{
/*还需要继续接受*/
stateRecv = STAT_RECV_REAR;
recvedLen = 0;
sprintf(buff,"0x%02x",recvBuff[0]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam1Return),buff);
sprintf(buff,"0x%02x",recvBuff[1]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam2Return),buff);
sprintf(buff,"0x%02x",recvBuff[2]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam3Return),buff);
sprintf(buff,"0x%02x",recvBuff[3]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam4Return),buff);
}
else if(recvRemainLen == 0)
{
stateRecv = STAT_RECV_BEGIN;
/*不需要接受了*/
if(recvedLen>=4)
{
sprintf(buff,"0x%02x",recvBuff[0]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam1Return),buff);
sprintf(buff,"0x%02x",recvBuff[1]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam2Return),buff);
sprintf(buff,"0x%02x",recvBuff[2]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam3Return),buff);
sprintf(buff,"0x%02x",recvBuff[3]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam4Return),buff);
}
else if(recvedLen==3)
{
sprintf(buff,"0x%02x",recvBuff[0]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam1Return),buff);
sprintf(buff,"0x%02x",recvBuff[1]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam2Return),buff);
sprintf(buff,"0x%02x",recvBuff[2]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam3Return),buff);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam4Return),DFT_CMD_RETURN);
}
else if(recvedLen==2)
{
sprintf(buff,"0x%02x",recvBuff[0]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam1Return),buff);
sprintf(buff,"0x%02x",recvBuff[1]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam2Return),buff);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam3Return),DFT_CMD_RETURN);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam4Return),DFT_CMD_RETURN);
}
else if(recvedLen==1)
{
sprintf(buff,"0x%02x",recvBuff[0]);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam1Return),buff);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam2Return),DFT_CMD_RETURN);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam3Return),DFT_CMD_RETURN);
SetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam4Return),DFT_CMD_RETURN);
}
}
}
if( (stateRecv==STAT_RECV_REAR) && (recvedLen==NET_MSG_MAXLEN))
{
recvedLen = 0;
}
}
}
CAsyncSocket::OnReceive(nErrorCode);
}
void CusrSkt::OnSend(int nErrorCode)
{
char buff[20];
int cmdLen,fileLen,readFileLen;
int fh;
struct _stat statBuf;
char fName[255];
// TODO: Add your specialized code here and/or call the base class
if(netSendSource == NET_SEND_LONG)
{
if(stateSend == STAT_SEND_BEGIN)
{
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdNumLong),buff,7);
sendBuff[0] = charToNum(buff[2],buff[3]);
sendBuff[1] = charToNum(buff[4],buff[5]);
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdLenLong),buff,20);
cmdLen = atoi(buff);
/*如果帧长度小于或者等于头部的长度,则发送文件全部,否则发送部分*/
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdFileLong),fName,255);
if((fh=_open(fName, _O_RDONLY)) ==-1)
{
MessageBox(NULL,"硬盘上不存在此文件,请重新输入文件名",NULL,MB_OK);
AsyncSelect(FD_READ | FD_CLOSE);
return;
}
else
{
if(_fstat(fh, &statBuf)==0)
{
fileLen = statBuf.st_size;
}
else
{
AsyncSelect(FD_READ | FD_CLOSE);
return;
}
_close(fh);
}
if((cmdLen<fileLen+NET_CMD_HEADLEN) && (cmdLen>NET_CMD_HEADLEN))
fileLen = cmdLen - NET_CMD_HEADLEN;
else
cmdLen = fileLen + NET_CMD_HEADLEN;
sendBuff[2] = ((cmdLen>>24)&0xFF);
sendBuff[3] = ((cmdLen>>16)&0xFF);
sendBuff[4] = ((cmdLen>>8)&0xFF);
sendBuff[5] = (cmdLen&0xFF);
sendBuff[6] =0;
sendBuff[7] =0;
if(netSendSize(sendBuff,NET_CMD_HEADLEN)!=TRUE)
{
AsyncSelect(FD_CLOSE);
return;
}
else
{
stateSend = STAT_SEND_HEAD;
sendRemainLen = fileLen;
sentLen = 0;
AsyncSelect(FD_WRITE);
}
}
else if((stateSend == STAT_SEND_HEAD) || (stateSend == STAT_SEND_REAR))
{
if(stateSend == STAT_SEND_HEAD)
{
/*打开文件*/
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdFileLong),fName,255);
pFile = fopen(fName, "rb");
stateSend = STAT_SEND_REAR;
}
if(sendRemainLen>=NET_MSG_MAXLEN)
{
readFileLen = NET_MSG_MAXLEN;
}
else
{
readFileLen = sendRemainLen;
}
fread(sendBuff,sizeof(char),readFileLen,pFile);
if(netSendSize(sendBuff,readFileLen)!=TRUE)
{
fclose(pFile);
AsyncSelect(FD_CLOSE);
return;
}
else
{
sendRemainLen = sendRemainLen - readFileLen;
}
if(sendRemainLen==0)
{
AsyncSelect(FD_READ | FD_CLOSE);
fclose(pFile);
EnableWindow(GetDlgItem(theApp.m_pMainWnd->m_hWnd,ID_cmdSendLong),TRUE);
EnableWindow(GetDlgItem(theApp.m_pMainWnd->m_hWnd,ID_cmdSendShort),TRUE);
}
else
{
AsyncSelect(FD_WRITE);
}
}
}
else if(netSendSource==NET_SEND_SHORT)
{
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdNumShort),buff,7);
sendBuff[0] = charToNum(buff[2],buff[3]);
sendBuff[1] = charToNum(buff[4],buff[5]);
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdLenShort),buff,20);
cmdLen = atoi(buff);
if( (cmdLen<NET_CMD_HEADLEN) || (cmdLen>NET_CMD_HEADLEN+4) )
{
cmdLen = NET_CMD_HEADLEN+4;
}
sendBuff[2] = ((cmdLen>>24)&0xFF);
sendBuff[3] = ((cmdLen>>16)&0xFF);
sendBuff[4] = ((cmdLen>>8)&0xFF);
sendBuff[5] = (cmdLen&0xFF);
sendBuff[6] =0;
sendBuff[7] =0;
if(cmdLen == NET_CMD_HEADLEN+1)
{
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam1Short),buff,7);
sendBuff[8] = charToNum(buff[2],buff[3]);
}
else if(cmdLen == NET_CMD_HEADLEN+2)
{
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam1Short),buff,7);
sendBuff[8] = charToNum(buff[2],buff[3]);
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam2Short),buff,7);
sendBuff[9] = charToNum(buff[2],buff[3]);
}
else if(cmdLen == NET_CMD_HEADLEN+3)
{
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam1Short),buff,7);
sendBuff[8] = charToNum(buff[2],buff[3]);
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam2Short),buff,7);
sendBuff[9] = charToNum(buff[2],buff[3]);
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam3Short),buff,7);
sendBuff[10] = charToNum(buff[2],buff[3]);
}
else if(cmdLen == NET_CMD_HEADLEN+4)
{
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam1Short),buff,7);
sendBuff[8] = charToNum(buff[2],buff[3]);
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam2Short),buff,7);
sendBuff[9] = charToNum(buff[2],buff[3]);
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam3Short),buff,7);
sendBuff[10] = charToNum(buff[2],buff[3]);
GetWindowText(GetDlgItem(theApp.m_pMainWnd->m_hWnd,IDC_cmdParam4Short),buff,7);
sendBuff[11] = charToNum(buff[2],buff[3]);
}
/*发送*/
if(netSendSize(sendBuff,cmdLen)!=TRUE)
{
AsyncSelect(FD_CLOSE);
return;
}
AsyncSelect(FD_READ | FD_CLOSE);
EnableWindow(GetDlgItem(theApp.m_pMainWnd->m_hWnd,ID_cmdSendLong),TRUE);
EnableWindow(GetDlgItem(theApp.m_pMainWnd->m_hWnd,ID_cmdSendShort),TRUE);
}
CAsyncSocket::OnSend(nErrorCode);
}
BOOL CusrSkt::netRecvSize(unsigned char *pBuff, int len)
{
int recvLen,remainLen;
remainLen = len;
while(remainLen>0)
{
recvLen = Receive(pBuff+len-remainLen, remainLen,0);
if( (recvLen==ERROR) || (recvLen ==0) )
{
return(FALSE);
}
remainLen = remainLen-recvLen;
}
return(TRUE);
}
BOOL CusrSkt::netSendSize(unsigned char *pBuff, int len)
{
int remainLen,realLen;
/*循环发送,直到发送完毕或者出错为止*/
remainLen = len;
while(remainLen>0)
{
if(remainLen>NET_MSG_MAXLEN)
{
Send(pBuff,NET_MSG_MAXLEN,0);
realLen = NET_MSG_MAXLEN;
}
else
{
Send(pBuff,remainLen,0);
realLen = remainLen;
}
if(realLen== ERROR)
{
return(FALSE);
}
remainLen = remainLen-realLen;
}
return(TRUE);
}
char CusrSkt::charToNum(char char1,char char2)
{
int tmpt1,tmpt2;
if(char1>='A'&&char1<='F')
{
tmpt1 = char1 - 'A'+0x0A;
}
else if(char1>='a'&&char1<='f')
{
tmpt1 = char1-'a'+0x0A;
}
else if(char1>='0'&&char1<='9')
{
tmpt1 = char1 - '0';
}
if(char2>='A'&&char2<='F')
{
tmpt2 = char2 - 'A'+0x0A;
}
else if(char2>='a'&&char2<='f')
{
tmpt2 = char2-'a'+0x0A;
}
else if(char2>='0'&&char2<='9')
{
tmpt2 = char2 - '0';
}
return( (tmpt1*16+tmpt2)&0xFF);
}
char CusrSkt::numToChar(char char1)
{
if(char1>=0&&char1<=9)
return(char1+'0');
else if(char1>=0xA && char1<=0xF)
return(char1-0xA+'A');
else
return('X');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -