forked from iversc/lbnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-udp-client-from.bas
More file actions
190 lines (150 loc) · 4.24 KB
/
test-udp-client-from.bas
File metadata and controls
190 lines (150 loc) · 4.24 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
call OpenLBNetDLL
input "press ENTER to begin.";a
connectServer$ = "localhost"
hSock = UDPConnectFrom(connectServer$, "50000", 0, "1600")
if IsSocketInvalid(hSock) then
print "Connect() failed. - ";GetError()
goto [doEnd]
end if
print "UDPConnect() successful."
data$ = "end"
lenData = len(data$)
print "UDPSend() - ";UDPSend(hSock, data$, lenData)
[respLoop]
timer 0
print "Waiting for response..."
ret = UDPIsReadAvailable(hSock, 0)
if ret = 0 then
print "Still waiting..."
timer 1000, [respLoop]
wait
end if
[doReceive]
bufLen = 1024
buf$ = space$(bufLen)
num = UDPReceive(hSock, buf$, bufLen)
theError = GetError()
if num = -1 then
if theError = 10101 or theError = 10054 then
print "Connection closed or reset by peer."
goto [recvLoop]
else
'With UDP, if a datagram comes in that is too large for the network stack
'or the specified buffer to handle, the data will be truncated, and the extra data
'is lost. It will still return as many bytes as it can, but it will generate
'the error code WSAEMSGSIZE(10040) while doing so.
'
'While the lost data is unrecoverable, this will at least let the application know
'something was lost.
if theError = 10040 then
print "Received message too large for buffer or other network limit. Datagram truncated, extra data lost."
else
print "Socket error occurred. - ";theError
end if
end if
goto [doClose]
end if
print "Response - ";left$(buf$, num)
[doClose]
a = UDPClose(hSock)
[doEnd]
call CloseLBNetDLL
'====================
'==Helper Functions==
'====================
Sub OpenLBNetDLL
open "LBNet.dll" for DLL as #LBNet
a = InitLBNet()
End Sub
Sub CloseLBNetDLL
a = EndLBNet()
close #LBNet
End Sub
Function InitLBNet()
CallDLL #LBNet, "InitLBNet",_
InitLBNet as long
End Function
Function EndLBNet()
CallDLL #LBNet, "EndLBNet",_
EndLBNet as long
End Function
Function IsSocketInvalid(sock)
CallDLL #LBNet, "IsSocketInvalid",_
sock as ulong,_
IsSocketInvalid as long
End Function
Function GetError()
CallDLL #LBNet, "GetError",_
GetError as long
if GetError < 0 then
GetError = (abs(GetError) XOR hexdec("FFFFFFFF")) + 1
end if
End Function
Function UDPConnect(host$, srv$, msTimeout)
CallDLL #LBNet, "UDPConnect",_
host$ as ptr,_
srv$ as ptr,_
msTimeout as long,_
UDPConnect as long
End Function
Function UDPConnectFrom(host$, srv$, msTimeout, localSrv$)
CallDLL #LBNet, "UDPConnectFrom",_
host$ as ptr,_
srv$ as ptr,_
msTimeout as long,_
localSrv$ as ptr,_
UDPConnectFrom as long
End Function
Function UDPSend(udpSock, buf$, bufLen)
CallDLL #LBNet, "UDPSend",_
udpSock as long,_
buf$ as ptr,_
bufLen as long,_
0 as long,_
UDPSend as long
End Function
Function UDPSendTo(udpSock, buf$, bufLen, udpInfo$)
CallDLL #LBNet, "UDPSend",_
udpSock as long,_
buf$ as ptr,_
bufLen as long,_
udpInfo$ as ptr,_
UDPSendTo as long
End Function
Function UDPClose(udpSock)
CallDLL #LBNet, "UDPClose",_
udpSock as long,_
UDPClose as long
End Function
Function UDPGetInfoSize()
CallDLL #LBNet, "UDPGetInfoSize",_
UDPGetInfoSize as long
End Function
Function UDPReceive(udpSock, byref buf$, bufLen)
CallDLL #LBNet, "UDPReceive",_
udpSock as long,_
buf$ as ptr,_
bufLen as long,_
0 as long,_
UDPReceive as long
End Function
Function UDPReceiveFrom(udpSock, byref buf$, bufLen, byref udpFrom$)
udpFrom$ = space$(UDPGetInfoSize())
CallDLL #LBNet, "UDPReceive",_
udpSock as long,_
buf$ as ptr,_
bufLen as long,_
udpFrom$ as ptr,_
UDPReceiveFrom as long
End Function
Function UDPIsReadAvailable(udpSock, msTimeout)
CallDLL #LBNet, "UDPIsReadAvailable",_
udpSock as long,_
msTimeout as long,_
UDPIsReadAvailable as long
End Function
Function UDPCreateListenSocket(pService$)
CallDLL #LBNet, "UDPCreateListenSocket",_
pService$ as ptr,_
UDPCreateListenSocket as long
End Function