CSTest/网络客户端/SocketC.h

46 lines
802 B
C++

#pragma once
#include<iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <thread>
#include <mutex>
#include <functional>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
class SocketC
{
private:
struct MsgHead
{
int MyIndex = 0;
int bufLen = 0;
time_t tm = 0;
char token[34] = { 0 };
};
char tmpBuf[10240] = { 0 };
int bufLen = 0;
SOCKET sclient = 0;
BOOL state = 0;
std::thread rec;
std::mutex msgLock;
function<VOID(char*, int)> Rfunc = NULL;
function<VOID()> Cfunc = NULL;
bool initSocket();
VOID Receive();
char* RecvBuff(char* buf, int& len);
public:
~SocketC();
bool Connect(string IP, UINT Prot);
int Send(const char* Date, int len);
VOID SetRecvFunc(function<VOID(char*, int)> fun);
VOID SetCloseFunc(function<VOID()> fun);
void Close();
};