admin
2022-07-22 9adb473067a993b4e0eacf3675baf14b29da3eca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once
#include<WinSock2.h> 
#include <WS2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <list>
#include <iostream>
#include "framework.h"
#include <atlstr.h>
using namespace std;
 
#define LOCAL_SERVER_ADDR "127.0.0.1"
#define LOCAL_SERVER_PORT 9006
 
typedef bool (*ActionCallback)(string data, void* contex);
 
class SocketManager
{
 
 
private:
    list<SOCKET> sockClients;
    static SOCKET socketServer;
    static void runSocketServer(void* context);
    static void processMsg(SOCKET  client, void* context);
    const int PORT = 9001;
    const char *ADDR = "127.0.0.1";
    sockaddr_in clientaddr;
    SOCKET getClient(int p);
    //Ìí¼Ó¿Í»§¶Ë
    void addClient(int p);
 
    static void* callbackContext;
    static ActionCallback actionCallback;
 
public:
    SocketManager(ActionCallback callback,void * context);
    ~SocketManager();
 
    void init(ActionCallback callback, void* contex);
 
    int getClientsNum();
 
    void resetClient(int p);
 
    //Á¬½ÓËùÓÐ
    int Connect();
    int Connect(int p);
 
    //¶Ï¿ªËùÓÐ
    void disConnect();
    int disConnect(int p);
 
    BOOL sendMsg(int, const char *);
 
    static BOOL sendMsg(const char*);
 
 
};