Advertisement
thedarkfreak

LB Winsock UDP Client demo

Jan 10th, 2014
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. gosub [structInit]
  2.  
  3. Call OpenWinsock
  4. hostName$ = "localhost" 'name or IP address
  5. port = 27015
  6. sock = OpenUDPClientSocket(hostName$, port)
  7.  
  8. sendMsg$ = "END"
  9. a = send(sock, sendMsg$, len(sendMsg$))
  10.  
  11. a = closesocket(sock)
  12. call CloseWinsock
  13.  
  14. Function OpenUDPServSocket(Port)
  15. 'Attempt to get a local address for hosting
  16. err = getsrvaddrinfo(str$(Port))
  17. if err <> 0 then
  18. Notice "getaddrinfo failed: 0x";dechex$(err);" ";err
  19. a = WSACleanup()
  20. Call CloseWinsock
  21. end
  22. End If
  23.  
  24. 'Fill the addrinfo struct with the info returned in the pointer
  25. lpAddrinfo = lp.addrinfo.struct
  26. size = len(addrinfo.struct)
  27. CallDLL #kernel32, "RtlMoveMemory",_
  28. addrinfo as struct,_
  29. lpAddrinfo as ulong,_
  30. size as long,_
  31. ret as void
  32.  
  33. 'Fill the sockaddr struct from the returned addrinfo struct
  34. lpSockaddr = addrinfo.aiAddr.struct
  35. size = len(sockaddr.struct)
  36. CallDLL #kernel32, "RtlMoveMemory",_
  37. sockaddr as struct,_
  38. lpSockaddr as ulong,_
  39. size as long,_
  40. ret as void
  41.  
  42. 'Create the socket with the types returned from getaddrinfo
  43. sock = socket(addrinfo.aiFamily.struct, addrinfo.aiSocktype.struct, addrinfo.aiProtocol.struct)
  44. If sock = hexdec("FFFFFFFF") then
  45. err = WSAGetLastError()
  46. Notice "Socket creation failed: 0x";dechex$(err);" ";err
  47. Call freeaddrinfo
  48. a = WSACleanup()
  49. call CloseWinsock
  50. end
  51. End If
  52.  
  53. 'Bind the port
  54. If bind(sock, sockaddr.struct, addrinfo.aiAddrlen.struct) <> 0 then
  55. err = WSAGetLastError()
  56. Notice "Bind failed: 0x";dechex$(err);" ";err
  57. call freeaddrinfo
  58. a = closesocket(sock)
  59. a = WSACleanup()
  60. close #ws2
  61. end
  62. End If
  63.  
  64.  
  65. call freeaddrinfo 'addrinfo no longer needed
  66. OpenUDPServSocket = sock
  67. End Function
  68.  
  69. Function OpenUDPClientSocket(addr$, port)
  70. err = getaddrinfo(addr$, str$(port))
  71. If err <> 0 then
  72. Notice "getaddrinfo failed: 0x";dechex$(err);" ";err
  73. a = WSACleanup()
  74. Call CloseWinsock
  75. end
  76. End If
  77.  
  78. 'Fill the addrinfo struct with the info returned in the pointer
  79. lpAddrinfo = lp.addrinfo.struct
  80. size = len(addrinfo.struct)
  81. CallDLL #kernel32, "RtlMoveMemory",_
  82. addrinfo as struct,_
  83. lpAddrinfo as ulong,_
  84. size as long,_
  85. ret as void
  86.  
  87. 'Fill the sockaddr struct from the returned addrinfo struct
  88. lpSockaddr = addrinfo.aiAddr.struct
  89. size = len(sockaddr.struct)
  90. CallDLL #kernel32, "RtlMoveMemory",_
  91. sockaddr as struct,_
  92. lpSockaddr as ulong,_
  93. size as long,_
  94. ret as void
  95.  
  96. 'Create the socket with the types returned from getaddrinfo
  97. sock = socket(addrinfo.aiFamily.struct, addrinfo.aiSocktype.struct, addrinfo.aiProtocol.struct)
  98. If sock = hexdec("FFFFFFFF") then
  99. err = WSAGetLastError()
  100. Notice "Socket creation failed: 0x";dechex$(err);" ";err
  101. Call freeaddrinfo
  102. a = WSACleanup()
  103. call CloseWinsock
  104. end
  105. End If
  106.  
  107. 'Bind the port
  108. If connect(sock, sockaddr.struct, addrinfo.aiAddrlen.struct) <> 0 then
  109. err = WSAGetLastError()
  110. Notice "connect failed: 0x";dechex$(err);" ";err
  111. call freeaddrinfo
  112. a = closesocket(sock)
  113. a = WSACleanup()
  114. close #ws2
  115. end
  116. End If
  117.  
  118.  
  119. call freeaddrinfo 'addrinfo no longer needed
  120. OpenUDPClientSocket = sock
  121. End Function
  122.  
  123. Function DoRecv$(sock, ByRef IPAddr$)
  124. buf$ = space$(1024)
  125. ret = recvfrom(sock, buf$, 1024, 0)
  126. If (ret = -1) then
  127. err = WSAGetLastError()
  128. Notice "recvfrom error: 0x";dechex$(err);" ";err
  129. a = closesocket(sock)
  130. a = WSACleanup()
  131. Call CloseWinsock
  132. end
  133. End If
  134.  
  135. DoRecv$ = left$(buf$, ret)
  136.  
  137. IPAddr$ = winstring(inetntoa(sockaddrin.sinAddr.struct))
  138. End Function
  139.  
  140. Sub OpenWinsock
  141. open "ws2_32" for DLL as #ws2
  142. err = WSAStartup()
  143. if err <> 0 then
  144. Notice "WSAStartup failed: 0x";dechex$(err);" ";err
  145. Call CloseWinsock
  146. end
  147. end if
  148. End Sub
  149.  
  150. Sub CloseWinsock
  151. a = WSACleanup()
  152. close #ws2
  153. End Sub
  154.  
  155. Function recvfrom(sock, byref buf$, bufLen, flags)
  156. struct a, b as long
  157. a.b.struct = len(sockaddrin.struct)
  158.  
  159. CallDLL #ws2, "recvfrom",_
  160. sock as ulong,_
  161. buf$ as ptr,_
  162. bufLen as long,_
  163. flags as long,_
  164. sockaddrin as struct,_
  165. a as struct,_
  166. recvfrom as long
  167. End Function
  168.  
  169. Function recv(sock, byref buf$, bufLen)
  170. CallDLL #ws2, "recv",_
  171. sock as ulong,_
  172. buf$ as ptr,_
  173. bufLen as long,_
  174. 0 as long,_
  175. recv as long
  176. End Function
  177.  
  178. Function sendto(sock, buf$, bufLen)
  179. size = len(sockaddrin.struct)
  180. CallDLL #ws2, "sendto",_
  181. sock as ulong,_
  182. buf$ as ptr,_
  183. bufLen as long,_
  184. sockaddrin as struct,_
  185. size as long,_
  186. sendto as long
  187. End Function
  188.  
  189. Function inetntoa(inAddr)
  190. CallDLL #ws2, "inet_ntoa",_
  191. inAddr as ulong,_
  192. inetntoa as ulong
  193. End Function
  194.  
  195. Function send(socket, Buf$, bufLen)
  196. CallDLL #ws2, "send",_
  197. socket as ulong,_
  198. Buf$ as ptr,_
  199. bufLen as long,_
  200. 0 as long,_
  201. send as long
  202. End Function
  203.  
  204. Function bind(socket, byref lpSockAddr, namelen)
  205. CallDLL #ws2, "bind",_
  206. socket as ulong,_
  207. sockaddr as struct,_
  208. namelen as long,_
  209. bind as long
  210. End Function
  211.  
  212. Function connect(socket, byref lpSockAddr, namelen)
  213. CallDLL #ws2, "connect",_
  214. socket as ulong,_
  215. sockaddr as struct,_
  216. namelen as long,_
  217. connect as long
  218. End Function
  219.  
  220. Function socket(family, socktype, protocol)
  221. CallDLL #ws2, "socket",_
  222. family as long,_
  223. socktype as long,_
  224. protocol as long,_
  225. socket as ulong
  226. End Function
  227.  
  228. Function WSAGetLastError()
  229. CallDLL #ws2, "WSAGetLastError",_
  230. WSAGetLastError as long
  231. End Function
  232.  
  233. Function closesocket(sock)
  234. CallDLL #ws2, "closesocket",_
  235. sock as ulong,_
  236. closesocket as long
  237. End Function
  238.  
  239. Function getsrvaddrinfo(pServiceName$)
  240. CallDLL #ws2, "getaddrinfo",_
  241. 0 as ulong,_
  242. pServiceName$ as ptr,_
  243. HINTS as struct,_
  244. lp as struct,_
  245. getsrvaddrinfo as long
  246. End Function
  247.  
  248. Function getaddrinfo(host$, pServiceName$)
  249. CallDLL #ws2, "getaddrinfo",_
  250. host$ as ptr,_
  251. pServiceName$ as ptr,_
  252. HINTS as struct,_
  253. lp as struct,_
  254. getaddrinfo as long
  255. End Function
  256.  
  257. Sub freeaddrinfo
  258. CallDLL #ws2, "freeaddrinfo",_
  259. lp.addrinfo.struct as ulong,_
  260. ret as void
  261. End Sub
  262.  
  263. Function WSAStartup()
  264. word = MAKEWORD(2, 2)
  265. CallDLL #ws2, "WSAStartup",_
  266. word as word,_
  267. WSADATA as struct,_
  268. WSAStartup as long
  269. End Function
  270.  
  271. Function WSACleanup()
  272. CallDLL #ws2, "WSACleanup",_
  273. WSACleanup as long
  274. End Function
  275.  
  276. Function MAKEWORD(loWord, hiWord)
  277. MAKEWORD = (loWord AND hexdec("FF")) + (hiWord * hexdec("100"))
  278. End Function
  279.  
  280. Function LOBYTE(word)
  281. LOWORD = word AND hexdec("FF")
  282. End Function
  283.  
  284. Function HIWORD(word)
  285. HIWORD = (int(word / hexdec("100")) AND hexdec("FF"))
  286. End Function
  287.  
  288. Sub RtlCopyMemory lpDest, lpSource, uLength
  289. print "in sub"
  290.  
  291. 'CallDLL #kernel32, "SetHandleCount",_
  292. ' lpSource as ulong,_
  293. ' lpStruct as ulong
  294.  
  295. struct lp2, addrinfo as ulong
  296. lp2.addrinfo.struct = addrinfo.struct
  297.  
  298. CallDLL #kernel32, "RtlMoveMemory",_
  299. lp2 as struct,_
  300. lp.addrinfo.struct as ulong,_
  301. uLength as ulong,_
  302. ret as void
  303. End Sub
  304.  
  305. [structInit]
  306. struct WSADATA,_
  307. wVersion as word,_
  308. wHighVersion as word,_
  309. wsaDescription as CHAR[258],_
  310. wsaSystemStatus as char[130],_
  311. iMaxSockets as word,_
  312. iMaxUdpDg as word,_
  313. lpVendorInfo as ulong
  314.  
  315.  
  316. struct sockaddr,_
  317. saFamily as ushort,_
  318. saData as char[14]
  319.  
  320. struct addrinfo,_
  321. aiFlags as long,_
  322. aiFamily as long,_
  323. aiSocktype as long,_
  324. aiProtocol as long,_
  325. aiAddrlen as ulong,_
  326. aiCanonname as ulong,_
  327. aiAddr as ulong,_
  328. aiNext as ulong
  329.  
  330. struct lp,_
  331. addrinfo as ulong
  332.  
  333. 'addrinfo structure for hinting to getaddrinfo
  334. struct HINTS,_
  335. aiFlags as long,_
  336. aiFamily as long,_
  337. aiSocktype as long,_
  338. aiProtocol as long,_
  339. aiAddrlen as ulong,_
  340. aiCanonname as ulong,_
  341. aiAddr as ulong,_
  342. aiNext as ulong
  343.  
  344. struct SOCKET,_
  345. lp as ulong
  346.  
  347. struct sockaddrin,_
  348. sinFamily as short,_
  349. sinPort as ushort,_
  350. sinAddr as ulong,_
  351. sinZero as char[8]
  352.  
  353. HINTS.aiAddrlen.struct = 0
  354. HINTS.aiAddr.struct = 0
  355. HINTS.aiCanonname.struct = 0
  356. HINTS.aiNext.struct = 0
  357.  
  358. HINTS.aiFlags.struct = 1 'AI_PASSIVE
  359. HINTS.aiFamily.struct = 2 'AF_INET
  360. HINTS.aiSocktype.struct = 2 'SOCK_DGRAM (Use UDP datagrams instead of TCP stream)
  361. HINTS.aiProtocol.struct = 17 'IPPROTO_UDP
  362.  
  363. 'end
  364. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement