forked from jwcpp/jwEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_Tcp.cpp
More file actions
66 lines (52 loc) · 1.05 KB
/
Copy pathTest_Tcp.cpp
File metadata and controls
66 lines (52 loc) · 1.05 KB
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
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include "NetEvent.h"
#include "EventLoop.h"
#include "NetClient.h"
#include "NetServer.h"
#include "Timer.h"
#include "NetConnect.h"
#include "NetPacket.h"
class INetEvent : public NetEvent
{
public:
virtual void onAccept(NetConnect * conn){
if (conn)
{
// 服务器通知有客户端连入
conn->sendMsg(1, "12345", 5);
}
};
virtual void onConnect(NetConnect * conn, int argv){
if (conn)
{
// 客户端通知连接成功
}
};
virtual void onClose(NetConnect * conn){
};
virtual void onMsg(NetConnect * conn, int msgtype, NetPacket * pack){
std::string s;
(*pack) >> s;
printf("%s\n", s.c_str());
conn->sendMsg(1, pack);
}
};
//#define CLIENT_TEST
int main()
{
//初始化事件循环
INetEvent eve;
EventLoop::Instance()->init();
#ifdef CLIENT_TEST
NetClient client(EventLoop::Instance(), &eve);
client.connect("127.0.0.1", 3001);
#else
NetServer server(EventLoop::Instance(), &eve);
server.listen("127.0.0.1", 3001);
#endif
//开启事件循环
EventLoop::Instance()->run();
system("pause");
return 0;
}