-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtcpSrv.py
53 lines (43 loc) · 1.7 KB
/
tcpSrv.py
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
from XAsyncSockets import XAsyncSocketsPool, XAsyncTCPServer, XClosedReason
import time
def _onTCPSrvClientAccepted(xAsyncTCPServer, xAsyncTCPClient) :
global countAccepted
countAccepted += 1
print('%s) On TCP Server Client Accepted' % countAccepted)
xAsyncTCPClient.OnClosed = _onTCPClientClosed
xAsyncTCPClient.AsyncRecvData( onDataRecv = _onTCPClientDataRecv,
onDataRecvArg = 'test',
timeoutSec = 1 )
def _onTCPSrvClosed(xAsyncTCPServer, closedReason) :
print("On TCP Server Closed")
def _onTCPClientDataRecv(xAsyncTCPClient, data, arg) :
global countRecv
countRecv += 1
print( "%s) On TCP Client Data Recv (%s bytes) : %s" \
% (countRecv, len(data), data.tobytes()) )
xAsyncTCPClient.AsyncRecvData(onDataRecv=_onTCPClientDataRecv, timeoutSec=2)
def _onTCPClientClosed(xAsyncTCPClient, closedReason) :
global countClosed
countClosed += 1
if closedReason == XClosedReason.Error :
reason = "error"
elif closedReason == XClosedReason.ClosedByHost :
reason = "closed by host"
elif closedReason == XClosedReason.ClosedByPeer :
reason = "closed by peer"
elif closedReason == XClosedReason.Timeout :
reason = "timeout"
else :
reason = "???"
print("%s) On TCP Client Closed (%s)" % (countClosed, reason))
countAccepted = 0
countRecv = 0
countClosed = 0
pool = XAsyncSocketsPool()
srvAddr = ('0.0.0.0', 12345)
srv = XAsyncTCPServer.Create(pool, srvAddr)
srv.OnClientAccepted = _onTCPSrvClientAccepted
srv.OnClosed = _onTCPSrvClosed
pool.AsyncWaitEvents(threadsCount=1)
while True :
time.sleep(1)