Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gosub [structInit]
- Call OpenWinsock
- port = 27015
- sock = OpenUDPServSocket(port)
- while info$ <> "END"
- info$ = DoRecv$(sock, IPAddr$)
- Print IPAddr$;": ";info$
- wend
- a = closesocket(sock)
- call CloseWinsock
- '================================================
- '=========COMMON FUNCTIONS=======================
- '================================================
- Function OpenUDPServSocket(Port)
- 'Attempt to get a local address for hosting
- err = getsrvaddrinfo(str$(Port))
- if err <> 0 then
- Notice "getaddrinfo failed: 0x";dechex$(err);" ";err
- 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
- size = len(sockaddr.struct)
- CallDLL #kernel32, "RtlMoveMemory",_
- sockaddr as struct,_
- lpSockaddr as ulong,_
- size as long,_
- ret as void
- 'Create the socket with the types returned from getaddrinfo
- sock = socket(addrinfo.aiFamily.struct, addrinfo.aiSocktype.struct, addrinfo.aiProtocol.struct)
- If sock = hexdec("FFFFFFFF") then
- err = WSAGetLastError()
- Notice "Socket creation failed: 0x";dechex$(err);" ";err
- Call freeaddrinfo
- a = WSACleanup()
- call CloseWinsock
- end
- End If
- 'Bind the port
- If bind(sock, sockaddr.struct, addrinfo.aiAddrlen.struct) <> 0 then
- err = WSAGetLastError()
- Notice "Bind failed: 0x";dechex$(err);" ";err
- call freeaddrinfo
- a = closesocket(sock)
- a = WSACleanup()
- close #ws2
- end
- End If
- call freeaddrinfo 'addrinfo no longer needed
- OpenUDPServSocket = sock
- End Function
- Function OpenUDPClientSocket(addr$, port)
- err = getaddrinfo(addr$, str$(port))
- If err <> 0 then
- Notice "getaddrinfo failed: 0x";dechex$(err);" ";err
- 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
- size = len(sockaddr.struct)
- CallDLL #kernel32, "RtlMoveMemory",_
- sockaddr as struct,_
- lpSockaddr as ulong,_
- size as long,_
- ret as void
- 'Create the socket with the types returned from getaddrinfo
- sock = socket(addrinfo.aiFamily.struct, addrinfo.aiSocktype.struct, addrinfo.aiProtocol.struct)
- If sock = hexdec("FFFFFFFF") then
- err = WSAGetLastError()
- Notice "Socket creation failed: 0x";dechex$(err);" ";err
- Call freeaddrinfo
- a = WSACleanup()
- call CloseWinsock
- end
- End If
- 'Bind the port
- If connect(sock, sockaddr.struct, addrinfo.aiAddrlen.struct) <> 0 then
- err = WSAGetLastError()
- Notice "connect failed: 0x";dechex$(err);" ";err
- call freeaddrinfo
- a = closesocket(sock)
- a = WSACleanup()
- close #ws2
- end
- End If
- call freeaddrinfo 'addrinfo no longer needed
- OpenUDPClientSocket = sock
- End Function
- Function DoRecv$(sock, ByRef IPAddr$)
- buf$ = space$(1024)
- ret = recvfrom(sock, buf$, 1024, 0)
- If (ret = -1) then
- err = WSAGetLastError()
- Notice "recvfrom error: 0x";dechex$(err);" ";err
- a = closesocket(sock)
- a = WSACleanup()
- Call CloseWinsock
- end
- End If
- DoRecv$ = left$(buf$, ret)
- IPAddr$ = winstring(inetntoa(sockaddrin.sinAddr.struct))
- End Function
- 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 recvfrom(sock, byref buf$, bufLen, flags)
- struct a, b as long
- a.b.struct = len(sockaddrin.struct)
- CallDLL #ws2, "recvfrom",_
- sock as ulong,_
- buf$ as ptr,_
- bufLen as long,_
- flags as long,_
- sockaddrin as struct,_
- a as struct,_
- recvfrom as long
- End Function
- Function recv(sock, byref buf$, bufLen)
- CallDLL #ws2, "recv",_
- sock as ulong,_
- buf$ as ptr,_
- bufLen as long,_
- 0 as long,_
- recv as long
- End Function
- Function sendto(sock, buf$, bufLen)
- size = len(sockaddrin.struct)
- CallDLL #kernel32, "RtlMoveMemory",_
- sockaddr as struct,_
- sockaddrin as struct,_
- size as long,_
- ret as void
- size = len(sockaddrin.struct)
- CallDLL #ws2, "sendto",_
- sock as ulong,_
- buf$ as ptr,_
- bufLen as long,_
- sockaddr as struct,_
- size as long,_
- sendto as long
- End Function
- Function inetntoa(inAddr)
- CallDLL #ws2, "inet_ntoa",_
- inAddr as ulong,_
- inetntoa as ulong
- End Function
- Function send(socket, Buf$, bufLen)
- CallDLL #ws2, "send",_
- socket as ulong,_
- Buf$ as ptr,_
- bufLen as long,_
- 0 as long,_
- send as long
- End Function
- Function bind(socket, byref lpSockAddr, namelen)
- CallDLL #ws2, "bind",_
- socket as ulong,_
- sockaddr as struct,_
- namelen as long,_
- bind as long
- End Function
- Function connect(socket, byref lpSockAddr, namelen)
- CallDLL #ws2, "connect",_
- socket as ulong,_
- sockaddr as struct,_
- namelen as long,_
- connect 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 getsrvaddrinfo(pServiceName$)
- CallDLL #ws2, "getaddrinfo",_
- 0 as ulong,_
- pServiceName$ as ptr,_
- HINTS as struct,_
- lp as struct,_
- getsrvaddrinfo as long
- End Function
- Function getaddrinfo(host$, pServiceName$)
- CallDLL #ws2, "getaddrinfo",_
- host$ as ptr,_
- pServiceName$ as ptr,_
- HINTS as struct,_
- lp as struct,_
- getaddrinfo as long
- End Function
- Sub freeaddrinfo
- CallDLL #ws2, "freeaddrinfo",_
- lp.addrinfo.struct as ulong,_
- ret as void
- End Sub
- 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 RtlCopyMemory lpDest, lpSource, uLength
- print "in sub"
- 'CallDLL #kernel32, "SetHandleCount",_
- ' lpSource as ulong,_
- ' lpStruct as ulong
- struct lp2, addrinfo as ulong
- lp2.addrinfo.struct = addrinfo.struct
- CallDLL #kernel32, "RtlMoveMemory",_
- lp2 as struct,_
- lp.addrinfo.struct as ulong,_
- uLength 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 = 1 'AI_PASSIVE
- HINTS.aiFamily.struct = 2 'AF_INET
- HINTS.aiSocktype.struct = 2 'SOCK_DGRAM (Use UDP datagrams instead of TCP stream)
- HINTS.aiProtocol.struct = 17 'IPPROTO_UDP
- 'end
- return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement