CSTest/网络客户端/SocketC.h

46 lines
802 B
C
Raw Normal View History

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