Advertisement
thedarkfreak

LB UDP Client

Jan 21st, 2014
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. gosub [structInit]
  2. call OpenWinsock
  3.  
  4. HINTS.aiFlags.struct = 0
  5. Print "Doing getaddrinfo()..."
  6. CallDLL #ws2, "getaddrinfo",_
  7. "localhost" as ptr,_
  8. "27015" as ptr,_
  9. HINTS as struct,_
  10. lp as struct,_
  11. ret as long
  12.  
  13. If ret <> 0 then
  14. print "getaddrfailed: ";ret
  15. a = WSACleanup()
  16. call CloseWinsock
  17. end
  18. End If
  19.  
  20. 'Fill the addrinfo struct with the info returned in the pointer
  21. lpAddrinfo = lp.addrinfo.struct
  22. size = len(addrinfo.struct)
  23. CallDLL #kernel32, "RtlMoveMemory",_
  24. addrinfo as struct,_
  25. lpAddrinfo as ulong,_
  26. size as long,_
  27. ret as void
  28.  
  29. 'Fill the sockaddr struct from the returned addrinfo struct
  30. lpSockaddr = addrinfo.aiAddr.struct
  31. lpSockaddrSize = addrinfo.aiAddrlen.struct
  32. size = lpSockaddrSize
  33. CallDLL #kernel32, "RtlMoveMemory",_
  34. sockaddr as struct,_
  35. lpSockaddr as ulong,_
  36. size as long,_
  37. ret as void
  38.  
  39.  
  40. Print "Opening socket..."
  41. sock = socket(addrinfo.aiFamily.struct, addrinfo.aiSocktype.struct, addrinfo.aiProtocol.struct)
  42. If sock = hexdec("FFFFFFFF") then
  43. err = WSAGetLastError()
  44. print "socket creation failed: ";err
  45. call freeaddrinfo
  46. a = WSACleanup()
  47. call CloseWinsock
  48. end
  49. End If
  50.  
  51. Print "Attempting send..."
  52. testMsg$ = "END"
  53. size = len(testMsg$)
  54.  
  55. ret = sendto(sock, testMsg$, size, sockaddr.struct)
  56. If ret < 0 then
  57. print "send failed: ";WSAGetLastError()
  58. a = closesocket(sock)
  59. call freeaddrinfo
  60. a = WSACleanup()
  61. call CloseWinsock
  62. end
  63. End If
  64.  
  65.  
  66. buf$ = space$(1024)
  67. structSize = len(sockaddr.struct)
  68. CallDLL #ws2, "recvfrom",_
  69. sock as ulong,_
  70. buf$ as ptr,_
  71. 1024 as long,_
  72. 0 as long,_
  73. sockaddr as struct,_
  74. structSize as long,_
  75. ret as long
  76.  
  77. If ret < 0 then
  78. print "recv failed: ";WSAGetLastError()
  79. a = closesocket(sock)
  80. call freeaddrinfo
  81. a = WSACleanup()
  82. call CloseWinsock
  83. end
  84. End If
  85.  
  86. print left$(buf$, ret)
  87.  
  88. call freeaddrinfo
  89. a = WSACleanup()
  90. call CloseWinsock
  91. end
  92.  
  93. Sub OpenWinsock
  94. open "ws2_32" for DLL as #ws2
  95. err = WSAStartup()
  96. if err <> 0 then
  97. Notice "WSAStartup failed: 0x";dechex$(err);" ";err
  98. Call CloseWinsock
  99. end
  100. end if
  101. End Sub
  102.  
  103. Sub CloseWinsock
  104. a = WSACleanup()
  105. close #ws2
  106. End Sub
  107.  
  108. Function sendto(sock, buf$, bufLen, sockAddr$)
  109. c$ = sockAddr$
  110. size = len(c$)
  111.  
  112. CallDLL #ws2, "sendto",_
  113. sock as ulong,_
  114. buf$ as ptr,_
  115. bufLen as long,_
  116. 0 as long,_
  117. c$ as ptr,_
  118. size as long,_
  119. sendto as long
  120. End Function
  121.  
  122. Function socket(family, socktype, protocol)
  123. CallDLL #ws2, "socket",_
  124. family as long,_
  125. socktype as long,_
  126. protocol as long,_
  127. socket as ulong
  128. End Function
  129.  
  130. Function WSAGetLastError()
  131. CallDLL #ws2, "WSAGetLastError",_
  132. WSAGetLastError as long
  133. End Function
  134.  
  135. Function closesocket(sock)
  136. CallDLL #ws2, "closesocket",_
  137. sock as ulong,_
  138. closesocket as long
  139. End Function
  140.  
  141. Function WSAStartup()
  142. word = MAKEWORD(2, 2)
  143. CallDLL #ws2, "WSAStartup",_
  144. word as word,_
  145. WSADATA as struct,_
  146. WSAStartup as long
  147. End Function
  148.  
  149. Function WSACleanup()
  150. CallDLL #ws2, "WSACleanup",_
  151. WSACleanup as long
  152. End Function
  153.  
  154. Function MAKEWORD(loWord, hiWord)
  155. MAKEWORD = (loWord AND hexdec("FF")) + (hiWord * hexdec("100"))
  156. End Function
  157.  
  158. Function LOBYTE(word)
  159. LOWORD = word AND hexdec("FF")
  160. End Function
  161.  
  162. Function HIWORD(word)
  163. HIWORD = (int(word / hexdec("100")) AND hexdec("FF"))
  164. End Function
  165.  
  166. Sub freeaddrinfo
  167. CallDLL #ws2, "freeaddrinfo",_
  168. lp.addrinfo.struct as ulong,_
  169. ret as void
  170. End Sub
  171.  
  172. [structInit]
  173. struct WSADATA,_
  174. wVersion as word,_
  175. wHighVersion as word,_
  176. wsaDescription as CHAR[258],_
  177. wsaSystemStatus as char[130],_
  178. iMaxSockets as word,_
  179. iMaxUdpDg as word,_
  180. lpVendorInfo as ulong
  181.  
  182.  
  183. struct sockaddr,_
  184. saFamily as ushort,_
  185. saData as char[14]
  186.  
  187. struct addrinfo,_
  188. aiFlags as long,_
  189. aiFamily as long,_
  190. aiSocktype as long,_
  191. aiProtocol as long,_
  192. aiAddrlen as ulong,_
  193. aiCanonname as ulong,_
  194. aiAddr as ulong,_
  195. aiNext as ulong
  196.  
  197. struct lp,_
  198. addrinfo as ulong
  199.  
  200. 'addrinfo structure for hinting to getaddrinfo
  201. struct HINTS,_
  202. aiFlags as long,_
  203. aiFamily as long,_
  204. aiSocktype as long,_
  205. aiProtocol as long,_
  206. aiAddrlen as ulong,_
  207. aiCanonname as ulong,_
  208. aiAddr as ulong,_
  209. aiNext as ulong
  210.  
  211. struct SOCKET,_
  212. lp as ulong
  213.  
  214. struct sockaddrin,_
  215. sinFamily as short,_
  216. sinPort as ushort,_
  217. sinAddr as ulong,_
  218. sinZero as char[8]
  219.  
  220. HINTS.aiAddrlen.struct = 0
  221. HINTS.aiAddr.struct = 0
  222. HINTS.aiCanonname.struct = 0
  223. HINTS.aiNext.struct = 0
  224.  
  225. HINTS.aiFlags.struct = 0
  226. HINTS.aiFamily.struct = 2 'AF_INET
  227. HINTS.aiSocktype.struct = 2 'SOCK_DGRAM (Use UDP datagrams instead of TCP stream)
  228. HINTS.aiProtocol.struct = 0 'IPPROTO_UDP
  229.  
  230.  
  231. 'end
  232. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement