Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gosub [structInit]
- call OpenWinsock
- HINTS.aiFlags.struct = 0
- Print "Doing getaddrinfo()..."
- CallDLL #ws2, "getaddrinfo",_
- "localhost" as ptr,_
- "27015" as ptr,_
- HINTS as struct,_
- lp as struct,_
- ret as long
- If ret <> 0 then
- print "getaddrfailed: ";ret
- a = WSACleanup()
- call CloseWinsock
- end
- End If
- 'Fill the addrinfo struct with the info returned in the pointer
- lpAddrinfo = lp.addrinfo.struct
- size = len(addrinfo.struct)
- CallDLL #kernel32, "RtlMoveMemory",_
- addrinfo as struct,_
- lpAddrinfo as ulong,_
- size as long,_
- ret as void
- 'Fill the sockaddr struct from the returned addrinfo struct
- lpSockaddr = addrinfo.aiAddr.struct
- lpSockaddrSize = addrinfo.aiAddrlen.struct
- size = lpSockaddrSize
- CallDLL #kernel32, "RtlMoveMemory",_
- sockaddr as struct,_
- lpSockaddr as ulong,_
- size as long,_
- ret as void
- Print "Opening socket..."
- sock = socket(addrinfo.aiFamily.struct, addrinfo.aiSocktype.struct, addrinfo.aiProtocol.struct)
- If sock = hexdec("FFFFFFFF") then
- err = WSAGetLastError()
- print "socket creation failed: ";err
- call freeaddrinfo
- a = WSACleanup()
- call CloseWinsock
- end
- End If
- Print "Attempting send..."
- testMsg$ = "END"
- size = len(testMsg$)
- ret = sendto(sock, testMsg$, size, sockaddr.struct)
- If ret < 0 then
- print "send failed: ";WSAGetLastError()
- a = closesocket(sock)
- call freeaddrinfo
- a = WSACleanup()
- call CloseWinsock
- end
- End If
- buf$ = space$(1024)
- structSize = len(sockaddr.struct)
- CallDLL #ws2, "recvfrom",_
- sock as ulong,_
- buf$ as ptr,_
- 1024 as long,_
- 0 as long,_
- sockaddr as struct,_
- structSize as long,_
- ret as long
- If ret < 0 then
- print "recv failed: ";WSAGetLastError()
- a = closesocket(sock)
- call freeaddrinfo
- a = WSACleanup()
- call CloseWinsock
- end
- End If
- print left$(buf$, ret)
- call freeaddrinfo
- a = WSACleanup()
- call CloseWinsock
- end
- Sub OpenWinsock
- open "ws2_32" for DLL as #ws2
- err = WSAStartup()
- if err <> 0 then
- Notice "WSAStartup failed: 0x";dechex$(err);" ";err
- Call CloseWinsock
- end
- end if
- End Sub
- Sub CloseWinsock
- a = WSACleanup()
- close #ws2
- End Sub
- Function sendto(sock, buf$, bufLen, sockAddr$)
- c$ = sockAddr$
- size = len(c$)
- CallDLL #ws2, "sendto",_
- sock as ulong,_
- buf$ as ptr,_
- bufLen as long,_
- 0 as long,_
- c$ as ptr,_
- size as long,_
- sendto as long
- End Function
- Function socket(family, socktype, protocol)
- CallDLL #ws2, "socket",_
- family as long,_
- socktype as long,_
- protocol as long,_
- socket as ulong
- End Function
- Function WSAGetLastError()
- CallDLL #ws2, "WSAGetLastError",_
- WSAGetLastError as long
- End Function
- Function closesocket(sock)
- CallDLL #ws2, "closesocket",_
- sock as ulong,_
- closesocket as long
- End Function
- Function WSAStartup()
- word = MAKEWORD(2, 2)
- CallDLL #ws2, "WSAStartup",_
- word as word,_
- WSADATA as struct,_
- WSAStartup as long
- End Function
- Function WSACleanup()
- CallDLL #ws2, "WSACleanup",_
- WSACleanup as long
- End Function
- Function MAKEWORD(loWord, hiWord)
- MAKEWORD = (loWord AND hexdec("FF")) + (hiWord * hexdec("100"))
- End Function
- Function LOBYTE(word)
- LOWORD = word AND hexdec("FF")
- End Function
- Function HIWORD(word)
- HIWORD = (int(word / hexdec("100")) AND hexdec("FF"))
- End Function
- Sub freeaddrinfo
- CallDLL #ws2, "freeaddrinfo",_
- lp.addrinfo.struct as ulong,_
- ret as void
- End Sub
- [structInit]
- struct WSADATA,_
- wVersion as word,_
- wHighVersion as word,_
- wsaDescription as CHAR[258],_
- wsaSystemStatus as char[130],_
- iMaxSockets as word,_
- iMaxUdpDg as word,_
- lpVendorInfo as ulong
- struct sockaddr,_
- saFamily as ushort,_
- saData as char[14]
- struct addrinfo,_
- aiFlags as long,_
- aiFamily as long,_
- aiSocktype as long,_
- aiProtocol as long,_
- aiAddrlen as ulong,_
- aiCanonname as ulong,_
- aiAddr as ulong,_
- aiNext as ulong
- struct lp,_
- addrinfo as ulong
- 'addrinfo structure for hinting to getaddrinfo
- struct HINTS,_
- aiFlags as long,_
- aiFamily as long,_
- aiSocktype as long,_
- aiProtocol as long,_
- aiAddrlen as ulong,_
- aiCanonname as ulong,_
- aiAddr as ulong,_
- aiNext as ulong
- struct SOCKET,_
- lp as ulong
- struct sockaddrin,_
- sinFamily as short,_
- sinPort as ushort,_
- sinAddr as ulong,_
- sinZero as char[8]
- HINTS.aiAddrlen.struct = 0
- HINTS.aiAddr.struct = 0
- HINTS.aiCanonname.struct = 0
- HINTS.aiNext.struct = 0
- HINTS.aiFlags.struct = 0
- HINTS.aiFamily.struct = 2 'AF_INET
- HINTS.aiSocktype.struct = 2 'SOCK_DGRAM (Use UDP datagrams instead of TCP stream)
- HINTS.aiProtocol.struct = 0 'IPPROTO_UDP
- 'end
- return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement