diff --git a/网络客户端/SocketC.cpp b/网络客户端/SocketC.cpp index a3e8dcb..41ab085 100644 --- a/网络客户端/SocketC.cpp +++ b/网络客户端/SocketC.cpp @@ -1,5 +1,6 @@ #include "SocketC.h" +//ʼsocket bool SocketC::initSocket() { //ʼ׽ֿ @@ -23,11 +24,13 @@ bool SocketC::initSocket() } } -SocketC::SocketC(int bufSize, int modo) +// +SocketC::SocketC(int mode) { - + m_mode = mode; } +// SocketC::~SocketC() { if (state == 1) @@ -36,10 +39,11 @@ SocketC::~SocketC() } } +//ӵ bool SocketC::Connect(string IP, UINT Prot) { if (!initSocket()) - { + {//ʼʧ return false; } struct addrinfo* answer, hint; @@ -47,27 +51,25 @@ bool SocketC::Connect(string IP, UINT Prot) ZeroMemory(&hint, sizeof(hint)); hint.ai_family = AF_INET; hint.ai_socktype = SOCK_STREAM; - SOCKADDR_IN server_addr; - + //IP if (getaddrinfo(IP.c_str(), NULL, &hint, &answer) == 0) { server_addr.sin_family = AF_INET; - server_addr.sin_addr.S_un.S_addr = ((struct sockaddr_in*)(answer->ai_addr))->sin_addr.S_un.S_addr; server_addr.sin_port = htons(Prot); ((SOCKADDR_IN*)&hint.ai_addr)->sin_port = htons(Prot); - //׽ sclient = socket(AF_INET, SOCK_STREAM, 0); + //ӵ if (connect(sclient, (SOCKADDR*)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) - { + {//ʧ WSACleanup(); return false; } else - { + {//ӳɹ߳̽ state = 1; rec = std::thread((&SocketC::Receive), this); return true; @@ -76,66 +78,85 @@ bool SocketC::Connect(string IP, UINT Prot) return FALSE; } +//ݺ VOID SocketC::Receive() { char* buff = NULL; while(state) { + //ʼ char buf[1024] = { 0 }; - int len = recv(sclient, buf, 1024, 0); + int len = recv(sclient, buf, 1020, 0); if (len ==SOCKET_ERROR || len == 0) - { + {//ر state = 0; if (Cfunc != NULL) { Cfunc(); } - cout << "Ͽ"; + //cout << "Ͽ"; break; } - int isStr; - buff = RecvBuff(buf, len, isStr); - while (buff) - { + if (m_mode == 0) + {//ͨģʽֱӴ if (Rfunc != NULL) + Rfunc(buf, len); + } + else if (m_mode == 1) + {//յݱճ + int isStr; + buff = RecvBuff(buf, len, isStr); + while (buff) { - if (isStr == 1) + if (Rfunc != NULL) { - AES aes; - string str = aes.DecryptionAES(buff, "qwertyuiopasdfgh"); - Rfunc((char*)str.c_str(), str.length()); + if (isStr == 1) + {//ַȽ + AES aes; + string str = aes.DecryptionAES(buff, "qwertyuiopasdfgh"); + Rfunc((char*)str.c_str(), str.length()); + } + else + Rfunc(buff, len); } - else - Rfunc(buff, len); + delete buff; + len = 0; + buff = RecvBuff(NULL, len, isStr); } - delete buff; - len = 0; - buff = RecvBuff(NULL, len, isStr); } } return; } +//յ char* SocketC::RecvBuff(char* buf, int& len, int& isStr) { char* buff = NULL; MsgHead* h; - lock_guard guard(msgLock); + //lock_guard guard(msgLock); + //յݸƵ if (len != 0) memcpy_s(tmpBuf + bufLen, len, buf, len); bufLen += len; - + //жϻڵϢǷϢͷ if (bufLen > sizeof(MsgHead)) - { + {//Ϣͷ h = (MsgHead*)tmpBuf; + if (!CheckHead(h)) + {//ϢͷϹر + closesocket(sclient); + return NULL; + } + //жϵǰϢǷ if (bufLen - sizeof(MsgHead) >= h->bufLen) - { + {//ǰϢ buff = new char[h->bufLen + 1]; ZeroMemory(buff, h->bufLen + 1); memcpy_s(buff, h->bufLen, tmpBuf + sizeof(MsgHead), h->bufLen); len = h->bufLen; isStr = h->isStr; bufLen = bufLen - len - sizeof(MsgHead); + //ǰ if (bufLen > 0) memcpy_s(tmpBuf, bufLen, tmpBuf + sizeof(MsgHead) + len, bufLen); } @@ -143,53 +164,104 @@ char* SocketC::RecvBuff(char* buf, int& len, int& isStr) return buff; } - -int SocketC::SendData(const char* Date, int len, int isStr) +//Ϣͷ +BOOL SocketC::CheckHead(MsgHead* h) { - MsgHead h; - h.isStr = isStr; - h.bufLen = len; - time(&h.tm); - string str = to_string(h.tm); - str += to_string(len); - md5 md; - str = md.StringToMD5(str); - memcpy_s(h.token, str.length(), str.c_str(), str.length()); - char* buff = new char[sizeof(MsgHead) + len]; - memcpy_s(buff, sizeof(MsgHead), &h, sizeof(MsgHead)); - memcpy_s(buff + sizeof(MsgHead), len, Date, len); - int lenth = send(sclient, buff, len + sizeof(MsgHead), 0) - sizeof(MsgHead); - delete[] buff; - if (lenth < 0) + //ϢȲܴ10000 + if (h->bufLen > 10000) { - lenth = -1; + return FALSE; } - return lenth; + //ݲ֤TokenǷȷ + string str, str1; + str = to_string(h->tm); + str += to_string(h->bufLen); + md5 md; + str1 = md.StringToMD5(str); + str = h->token; + if (str == str1) + {//tokenȷ֤Ϣʱ + time_t t1; + time(&t1); + if (abs(t1 - h->tm) < 300) + { + return TRUE; + } + } + return FALSE; } +//ݰ +int SocketC::SendData(const char* Data, int len, int isStr) +{ + if (Data == NULL) + return -1; + if (m_mode == 0) + {//ͨģʽֱӷ + return send(sclient, Data, len, 0); + } + else if (m_mode == 1) + {//token + MsgHead h; + h.isStr = isStr; + h.bufLen = len; + time(&h.tm); + string str = to_string(h.tm); + str += to_string(len); + md5 md; + str = md.StringToMD5(str); + memcpy_s(h.token, str.length(), str.c_str(), str.length()); + char* buff = new char[sizeof(MsgHead) + len]; + memcpy_s(buff, sizeof(MsgHead), &h, sizeof(MsgHead)); + memcpy_s(buff + sizeof(MsgHead), len, Data, len); + // + int lenth = send(sclient, buff, len + sizeof(MsgHead), 0) - sizeof(MsgHead); + delete[] buff; + if (lenth < 0) + { + lenth = -1; + } + return lenth; + } +} + +//ַ BOOL SocketC::SendStr(string str) { if (str == "") return FALSE; - AES aes; - string sendStr = aes.EncryptionAES(str, "qwertyuiopasdfgh"); - if (-1 == SendData(sendStr.c_str(), sendStr.length(), 1)) - { - return FALSE; + if (m_mode == 0) + {//ͨģʽֱӷ + if (send(sclient, str.c_str(), str.length(), 0) == -1) + { + return FALSE; + } + } + else if (m_mode == 1) + {//ܷ͵ַ + AES aes; + string sendStr = aes.EncryptionAES(str, "qwertyuiopasdfgh"); + if (-1 == SendData(sendStr.c_str(), sendStr.length(), 1)) + { + return FALSE; + } } return TRUE; } +//ýݵĻص VOID SocketC::SetRecvFunc(function fun) { Rfunc = fun; } +//öϿӵĻص VOID SocketC::SetCloseFunc(function fun) { Cfunc = fun; } +//Ͽ void SocketC::Close() { if (state != 0) diff --git a/网络客户端/SocketC.h b/网络客户端/SocketC.h index 76f6edf..135b064 100644 --- a/网络客户端/SocketC.h +++ b/网络客户端/SocketC.h @@ -23,26 +23,26 @@ private: char token[34] = { 0 }; }; - char tmpBuf[10240] = { 0 }; - int bufLen = 0; - SOCKET sclient = 0; - BOOL state = 0; - std::thread rec; - std::mutex msgLock; - function Rfunc = NULL; - function Cfunc = NULL; - bool initSocket(); - VOID Receive(); - char* RecvBuff(char* buf, int& len, int& isStr); - + char tmpBuf[10240] = { 0 }; // + int bufLen = 0; //ǰ˶೤ + SOCKET sclient = 0; //socketͻ + BOOL state = 0; //ǰ״̬ + int m_mode; //ǰģʽ + std::thread rec; //ݵ߳ + function Rfunc = NULL; //յݵĻص + function Cfunc = NULL; //ϿӵĻص + bool initSocket(); //ʼsocket + VOID Receive(); //ݺ + char* RecvBuff(char* buf, int& len, int& isStr); //յ + BOOL CheckHead(MsgHead* h); //Ϣͷ public: - SocketC(int bufSize = 4096, int modo = 0); - ~SocketC(); - bool Connect(string IP, UINT Prot); - int SendData(const char* Date, int len, int isStr = 0); - BOOL SendStr(string str); - VOID SetRecvFunc(function fun); - VOID SetCloseFunc(function fun); - void Close(); + SocketC(int mode = 0); // + ~SocketC(); // + bool Connect(string IP, UINT Prot); //ӵ + int SendData(const char* Date, int len, int isStr = 0); //ݰ + BOOL SendStr(string str); //ַ + VOID SetRecvFunc(function fun); //ýݵĻص + VOID SetCloseFunc(function fun); //öϿӵĻص + void Close(); //Ͽ }; diff --git a/网络客户端/网络客户端.cpp b/网络客户端/网络客户端.cpp index ed3403a..bb3d2af 100644 --- a/网络客户端/网络客户端.cpp +++ b/网络客户端/网络客户端.cpp @@ -6,7 +6,7 @@ using namespace std; -SocketC c; +SocketC c(1); int flag = 1; int myName = 0; diff --git a/网络服务端/SocketS.cpp b/网络服务端/SocketS.cpp index d305536..bbf9f0e 100644 --- a/网络服务端/SocketS.cpp +++ b/网络服务端/SocketS.cpp @@ -15,33 +15,45 @@ DWORD WINAPI SocketS::ThreadProc(LPVOID lpParameter) while (server->g_flag) {//ȡɶ˿ڵ BOOL bFlag = GetQueuedCompletionStatus(port, &len, &index, &lpOverlapped, INFINITE); - server->Lock.lock(); + //server->Lock.lock(); clienInfo* clien = &server->ClienMap[index]; if (bFlag == TRUE && len > 0) {// յͻϢ - clien->Rbuflen += len; - DOSTART - if (clien->Rbuflen < sizeof(MsgHead)) - break; - if (clien->Rbuflen - sizeof(MsgHead) < clien->h.bufLen) - break; - if (clien->buff == NULL) - break; - if (server->Rfunc == NULL) - break; - if (clien->h.isStr == 1) - { - AES aes; - string str = aes.DecryptionAES(clien->buff, "qwertyuiopasdfgh"); - server->Rfunc(index, (char*)str.c_str(), str.length()); - } - else - server->Rfunc(index, clien->buff, len); - - ZeroMemory(clien->buff,sizeof(clien->buff)); - clien->Rbuflen = 0; - DOEND + if (server->m_mode == 0) + {//ͨģʽ + if (server->Rfunc != NULL) + { + server->Rfunc(index, clien->buff, len); + } + ZeroMemory(clien->buff, sizeof(clien->buff)); server->PostRecv(index); // ԼͶݽ + } + else if (server->m_mode == 1) + { + clien->Rbuflen += len; + DOSTART + if (clien->Rbuflen < sizeof(MsgHead)) + break; + if (clien->Rbuflen - sizeof(MsgHead) < clien->h.bufLen) + break; + if (clien->buff == NULL) + break; + if (server->Rfunc == NULL) + break; + if (clien->h.isStr == 1) + { + AES aes; + string str = aes.DecryptionAES(clien->buff, "qwertyuiopasdfgh"); + server->Rfunc(index, (char*)str.c_str(), str.length()); + } + else + server->Rfunc(index, clien->buff, len); + + ZeroMemory(clien->buff, sizeof(clien->buff)); + clien->Rbuflen = 0; + DOEND + server->PostRecv(index); // ԼͶݽ + } } else {//ͻ˹ر @@ -52,15 +64,22 @@ DWORD WINAPI SocketS::ThreadProc(LPVOID lpParameter) server->CloseClien(index); } - server->Lock.unlock(); + //server->Lock.unlock(); } server->Count--; return 0; } - -SocketS::SocketS() +// +SocketS::SocketS(int mode) { + m_mode = mode; +} + +// +SocketS::~SocketS() +{ + Close(); } //ͶϢ @@ -68,28 +87,37 @@ VOID SocketS::PostRecv(int index) { WSABUF wsabuf; clienInfo* clien = &ClienMap[index]; - if (clien->Rbuflen < sizeof(MsgHead)) - {//յϢûϢͷ - wsabuf.buf = (char*)&clien->h + clien->Rbuflen; - wsabuf.len = sizeof(MsgHead) - clien->Rbuflen; - } - else + if (m_mode == 0) { - if (clien->Rbuflen == sizeof(MsgHead)) - {//սϢͷ - if (!CheckHead(&clien->h)) - {//Ϣͷ - CloseClien(index); - return; - } - } - wsabuf.buf = clien->buff + (clien->Rbuflen - sizeof(MsgHead)); - //ʣϢ1020ôֿϢ - if (clien->h.bufLen - clien->Rbuflen + sizeof(MsgHead) > 1020) - wsabuf.len = 1020; - else - wsabuf.len = clien->h.bufLen - clien->Rbuflen + sizeof(MsgHead); + wsabuf.buf = clien->buff; + wsabuf.len = 1020; } + else if (m_mode == 1) + { + if (clien->Rbuflen < sizeof(MsgHead)) + {//յϢûϢͷ + wsabuf.buf = (char*)&clien->h + clien->Rbuflen; + wsabuf.len = sizeof(MsgHead) - clien->Rbuflen; + } + else + { + if (clien->Rbuflen == sizeof(MsgHead)) + {//սϢͷ + if (!CheckHead(&clien->h)) + {//Ϣͷ + CloseClien(index); + return; + } + } + wsabuf.buf = clien->buff + (clien->Rbuflen - sizeof(MsgHead)); + //ʣϢ1020ôֿϢ + if (clien->h.bufLen - clien->Rbuflen + sizeof(MsgHead) > 1020) + wsabuf.len = 1020; + else + wsabuf.len = clien->h.bufLen - clien->Rbuflen + sizeof(MsgHead); + } + } + //ɶ˿ڼ DWORD dwRecvCount = wsabuf.len; DWORD dwFlag = 0; @@ -217,6 +245,8 @@ BOOL SocketS::Creat(int Prot) //رշ void SocketS::Close() {//ͻ + if (g_flag == 0) + return; g_flag = 0; Lock.lock(); for (auto i = ClienMap.begin(); i != ClienMap.end(); i++) @@ -269,29 +299,40 @@ VOID SocketS::GetClienName(int index, string& IP, int& Prot) //ݰ int SocketS::SendData(int index,const char* buf, int len, int isStr) -{//Ϣͷtoken - MsgHead h; - h.bufLen = len; - h.isStr = isStr; - time(&h.tm); - string str = to_string(h.tm); - str += to_string(len); - md5 md; - str = md.StringToMD5(str); - memcpy_s(h.token, str.length(), str.c_str(), str.length()); - //ϲϢͷϢ - char* buff = new char[sizeof(MsgHead) + len]; - memcpy_s(buff, sizeof(MsgHead), &h, sizeof(MsgHead)); - memcpy_s(buff + sizeof(MsgHead), len, buf, len); - //Ϣ - int lenth = PostSend(index, buff, len + sizeof(MsgHead)) - sizeof(MsgHead); - delete[] buff; - if (lenth < 0) - {//Ϣʧ - CloseClien(index); - lenth = -1; +{ + if (buf == NULL) + return -1; + if (m_mode == 0) + {//ͨģʽ + return PostSend(index, buf, len); } - return lenth; + else if (m_mode == 1) + { + //Ϣͷtoken + MsgHead h; + h.bufLen = len; + h.isStr = isStr; + time(&h.tm); + string str = to_string(h.tm); + str += to_string(len); + md5 md; + str = md.StringToMD5(str); + memcpy_s(h.token, str.length(), str.c_str(), str.length()); + //ϲϢͷϢ + char* buff = new char[sizeof(MsgHead) + len]; + memcpy_s(buff, sizeof(MsgHead), &h, sizeof(MsgHead)); + memcpy_s(buff + sizeof(MsgHead), len, buf, len); + //Ϣ + int lenth = PostSend(index, buff, len + sizeof(MsgHead)) - sizeof(MsgHead); + delete[] buff; + if (lenth < 0) + {//Ϣʧ + CloseClien(index); + lenth = -1; + } + return lenth; + } + return -1; } //ַ @@ -299,13 +340,23 @@ BOOL SocketS::SendStr(int index, string str) {//жϢΪ if (str == "") return FALSE; - //ַ - AES aes; - string sendStr = aes.EncryptionAES(str,"qwertyuiopasdfgh"); - //Ϣ - if (-1 == SendData(index, sendStr.c_str(), sendStr.length(), 1)) - {//Ϣʧ - return FALSE; + if (m_mode == 0) + {//ͨģʽ + if (PostSend(index, str.c_str(), str.length()) == -1) + { + return FALSE; + } + } + else if (m_mode == 1) + { + //ַ + AES aes; + string sendStr = aes.EncryptionAES(str, "qwertyuiopasdfgh"); + //Ϣ + if (-1 == SendData(index, sendStr.c_str(), sendStr.length(), 1)) + {//Ϣʧ + return FALSE; + } } return TRUE; } diff --git a/网络服务端/SocketS.h b/网络服务端/SocketS.h index 4c10732..f71dbc4 100644 --- a/网络服务端/SocketS.h +++ b/网络服务端/SocketS.h @@ -45,6 +45,7 @@ private: std::thread *ListenClien; //߳ int Count = 0; //̶߳ٸ mutex Lock; //߳ + int m_mode; //ǰģʽ function Rfunc = NULL; //ݵĻص function Cfunc = NULL; //ͻ˹رյĻص @@ -55,7 +56,8 @@ private: VOID Listens(); //Ϣ public: - SocketS(); // + SocketS(int mode = 0); // + ~SocketS(); // BOOL Creat(int Prot); // void Close(); //رշ VOID SetRecvFunc(function fun); //ýϢĻص diff --git a/网络服务端/网络服务端.cpp b/网络服务端/网络服务端.cpp index 8cfd983..dd82b1e 100644 --- a/网络服务端/网络服务端.cpp +++ b/网络服务端/网络服务端.cpp @@ -29,7 +29,7 @@ struct msgInfo std::map clienMap; std::map clienIndex; -SocketS s; +SocketS s(1); int cont = 0; VOID closeFunc(int index) { @@ -52,8 +52,8 @@ VOID recvFunc(int index, char* buf, int len) int main(void) { //s.SetRecvFunc([&tt](SocketS::msgInfo info) {return tt.ppp(info); }); - s.Creat(6666); - s.Close(); + //s.Creat(6666); + //s.Close(); s.SetCloseFunc(closeFunc); s.SetRecvFunc(recvFunc); diff --git a/网页请求/SocketC.cpp b/网页请求/SocketC.cpp index a2e066f..41ab085 100644 --- a/网页请求/SocketC.cpp +++ b/网页请求/SocketC.cpp @@ -1,5 +1,6 @@ #include "SocketC.h" +//ʼsocket bool SocketC::initSocket() { //ʼ׽ֿ @@ -23,11 +24,13 @@ bool SocketC::initSocket() } } -SocketC::SocketC(int bufSize, int modo) +// +SocketC::SocketC(int mode) { - + m_mode = mode; } +// SocketC::~SocketC() { if (state == 1) @@ -36,10 +39,11 @@ SocketC::~SocketC() } } +//ӵ bool SocketC::Connect(string IP, UINT Prot) { if (!initSocket()) - { + {//ʼʧ return false; } struct addrinfo* answer, hint; @@ -47,27 +51,25 @@ bool SocketC::Connect(string IP, UINT Prot) ZeroMemory(&hint, sizeof(hint)); hint.ai_family = AF_INET; hint.ai_socktype = SOCK_STREAM; - SOCKADDR_IN server_addr; - + //IP if (getaddrinfo(IP.c_str(), NULL, &hint, &answer) == 0) { server_addr.sin_family = AF_INET; - server_addr.sin_addr.S_un.S_addr = ((struct sockaddr_in*)(answer->ai_addr))->sin_addr.S_un.S_addr; server_addr.sin_port = htons(Prot); ((SOCKADDR_IN*)&hint.ai_addr)->sin_port = htons(Prot); - //׽ sclient = socket(AF_INET, SOCK_STREAM, 0); + //ӵ if (connect(sclient, (SOCKADDR*)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) - { + {//ʧ WSACleanup(); return false; } else - { + {//ӳɹ߳̽ state = 1; rec = std::thread((&SocketC::Receive), this); return true; @@ -76,15 +78,17 @@ bool SocketC::Connect(string IP, UINT Prot) return FALSE; } +//ݺ VOID SocketC::Receive() { char* buff = NULL; while(state) { + //ʼ char buf[1024] = { 0 }; - int len = recv(sclient, buf, 1024, 0); + int len = recv(sclient, buf, 1020, 0); if (len ==SOCKET_ERROR || len == 0) - { + {//ر state = 0; if (Cfunc != NULL) { @@ -93,49 +97,66 @@ VOID SocketC::Receive() //cout << "Ͽ"; break; } - int isStr; - buff = RecvBuff(buf, len, isStr); - while (buff) - { + if (m_mode == 0) + {//ͨģʽֱӴ if (Rfunc != NULL) + Rfunc(buf, len); + } + else if (m_mode == 1) + {//յݱճ + int isStr; + buff = RecvBuff(buf, len, isStr); + while (buff) { - if (isStr == 1) + if (Rfunc != NULL) { - AES aes; - string str = aes.DecryptionAES(buff, "qwertyuiopasdfgh"); - Rfunc((char*)str.c_str(), str.length()); + if (isStr == 1) + {//ַȽ + AES aes; + string str = aes.DecryptionAES(buff, "qwertyuiopasdfgh"); + Rfunc((char*)str.c_str(), str.length()); + } + else + Rfunc(buff, len); } - else - Rfunc(buff, len); + delete buff; + len = 0; + buff = RecvBuff(NULL, len, isStr); } - delete buff; - len = 0; - buff = RecvBuff(NULL, len, isStr); } } return; } +//յ char* SocketC::RecvBuff(char* buf, int& len, int& isStr) { char* buff = NULL; MsgHead* h; - lock_guard guard(msgLock); + //lock_guard guard(msgLock); + //յݸƵ if (len != 0) memcpy_s(tmpBuf + bufLen, len, buf, len); bufLen += len; - + //жϻڵϢǷϢͷ if (bufLen > sizeof(MsgHead)) - { + {//Ϣͷ h = (MsgHead*)tmpBuf; + if (!CheckHead(h)) + {//ϢͷϹر + closesocket(sclient); + return NULL; + } + //жϵǰϢǷ if (bufLen - sizeof(MsgHead) >= h->bufLen) - { + {//ǰϢ buff = new char[h->bufLen + 1]; ZeroMemory(buff, h->bufLen + 1); memcpy_s(buff, h->bufLen, tmpBuf + sizeof(MsgHead), h->bufLen); len = h->bufLen; isStr = h->isStr; bufLen = bufLen - len - sizeof(MsgHead); + //ǰ if (bufLen > 0) memcpy_s(tmpBuf, bufLen, tmpBuf + sizeof(MsgHead) + len, bufLen); } @@ -143,53 +164,104 @@ char* SocketC::RecvBuff(char* buf, int& len, int& isStr) return buff; } - -int SocketC::SendData(const char* Date, int len, int isStr) +//Ϣͷ +BOOL SocketC::CheckHead(MsgHead* h) { - MsgHead h; - h.isStr = isStr; - h.bufLen = len; - time(&h.tm); - string str = to_string(h.tm); - str += to_string(len); - md5 md; - str = md.StringToMD5(str); - memcpy_s(h.token, str.length(), str.c_str(), str.length()); - char* buff = new char[sizeof(MsgHead) + len]; - memcpy_s(buff, sizeof(MsgHead), &h, sizeof(MsgHead)); - memcpy_s(buff + sizeof(MsgHead), len, Date, len); - int lenth = send(sclient, buff, len + sizeof(MsgHead), 0) - sizeof(MsgHead); - delete[] buff; - if (lenth < 0) + //ϢȲܴ10000 + if (h->bufLen > 10000) { - lenth = -1; + return FALSE; } - return lenth; + //ݲ֤TokenǷȷ + string str, str1; + str = to_string(h->tm); + str += to_string(h->bufLen); + md5 md; + str1 = md.StringToMD5(str); + str = h->token; + if (str == str1) + {//tokenȷ֤Ϣʱ + time_t t1; + time(&t1); + if (abs(t1 - h->tm) < 300) + { + return TRUE; + } + } + return FALSE; } +//ݰ +int SocketC::SendData(const char* Data, int len, int isStr) +{ + if (Data == NULL) + return -1; + if (m_mode == 0) + {//ͨģʽֱӷ + return send(sclient, Data, len, 0); + } + else if (m_mode == 1) + {//token + MsgHead h; + h.isStr = isStr; + h.bufLen = len; + time(&h.tm); + string str = to_string(h.tm); + str += to_string(len); + md5 md; + str = md.StringToMD5(str); + memcpy_s(h.token, str.length(), str.c_str(), str.length()); + char* buff = new char[sizeof(MsgHead) + len]; + memcpy_s(buff, sizeof(MsgHead), &h, sizeof(MsgHead)); + memcpy_s(buff + sizeof(MsgHead), len, Data, len); + // + int lenth = send(sclient, buff, len + sizeof(MsgHead), 0) - sizeof(MsgHead); + delete[] buff; + if (lenth < 0) + { + lenth = -1; + } + return lenth; + } +} + +//ַ BOOL SocketC::SendStr(string str) { if (str == "") return FALSE; - AES aes; - string sendStr = aes.EncryptionAES(str, "qwertyuiopasdfgh"); - if (-1 == SendData(sendStr.c_str(), sendStr.length(), 1)) - { - return FALSE; + if (m_mode == 0) + {//ͨģʽֱӷ + if (send(sclient, str.c_str(), str.length(), 0) == -1) + { + return FALSE; + } + } + else if (m_mode == 1) + {//ܷ͵ַ + AES aes; + string sendStr = aes.EncryptionAES(str, "qwertyuiopasdfgh"); + if (-1 == SendData(sendStr.c_str(), sendStr.length(), 1)) + { + return FALSE; + } } return TRUE; } +//ýݵĻص VOID SocketC::SetRecvFunc(function fun) { Rfunc = fun; } +//öϿӵĻص VOID SocketC::SetCloseFunc(function fun) { Cfunc = fun; } +//Ͽ void SocketC::Close() { if (state != 0) diff --git a/网页请求/SocketC.h b/网页请求/SocketC.h index 6a03db2..135b064 100644 --- a/网页请求/SocketC.h +++ b/网页请求/SocketC.h @@ -1,8 +1,16 @@ #pragma once -#include "pub.h" +#include +#include +#include +#include +#include +#include #include "md5.h" #include "AES.h" +#pragma comment(lib,"ws2_32.lib") +using namespace std; + class SocketC { private: @@ -15,26 +23,26 @@ private: char token[34] = { 0 }; }; - char tmpBuf[10240] = { 0 }; - int bufLen = 0; - SOCKET sclient = 0; - BOOL state = 0; - std::thread rec; - std::mutex msgLock; - function Rfunc = NULL; - function Cfunc = NULL; - bool initSocket(); - VOID Receive(); - char* RecvBuff(char* buf, int& len, int& isStr); - + char tmpBuf[10240] = { 0 }; // + int bufLen = 0; //ǰ˶೤ + SOCKET sclient = 0; //socketͻ + BOOL state = 0; //ǰ״̬ + int m_mode; //ǰģʽ + std::thread rec; //ݵ߳ + function Rfunc = NULL; //յݵĻص + function Cfunc = NULL; //ϿӵĻص + bool initSocket(); //ʼsocket + VOID Receive(); //ݺ + char* RecvBuff(char* buf, int& len, int& isStr); //յ + BOOL CheckHead(MsgHead* h); //Ϣͷ public: - SocketC(int bufSize = 4096, int modo = 0); - ~SocketC(); - bool Connect(string IP, UINT Prot); - int SendData(const char* Date, int len, int isStr = 0); - BOOL SendStr(string str); - VOID SetRecvFunc(function fun); - VOID SetCloseFunc(function fun); - void Close(); + SocketC(int mode = 0); // + ~SocketC(); // + bool Connect(string IP, UINT Prot); //ӵ + int SendData(const char* Date, int len, int isStr = 0); //ݰ + BOOL SendStr(string str); //ַ + VOID SetRecvFunc(function fun); //ýݵĻص + VOID SetCloseFunc(function fun); //öϿӵĻص + void Close(); //Ͽ }; diff --git a/网页请求/网页请求.cpp b/网页请求/网页请求.cpp index 7501a32..aa54848 100644 --- a/网页请求/网页请求.cpp +++ b/网页请求/网页请求.cpp @@ -3,7 +3,7 @@ #include "Json.h" #include "SocketC.h" -SocketC tcpc; +SocketC tcpc(1); char* U8ToUnicode(char* szU8) {