Advertisement
thedarkfreak

LB Winsock UDP Server demo

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