Post by konijn on Apr 13, 2019 12:18:52 GMT -5
Hello,
I have here a udp server software:
gosub [structInit]
Call OpenWinsock
port = 27015
sock = OpenUDPServSocket(port)
while info$ <> "END"
sckadrSize = len(sockaddr.struct)
sckadr$ = space$(sckadrSize)
recv = recvfrom(sock, buf$, 1024, 0, sckadr$, sckadrSize)
If recv < 0 then
print "recv error: ";WSAGetLastError()
a = closesocket(sock)
a = WSACleanup()
call CloseWinsock
end
end if
info$ = left$(buf$, recv)
print info$
sendBuf$ = "testReturn"
ret = sendto(sock, sendBuf$, len(sendBuf$), sckadr$)
if ret = -1 then
print "Send error - ";WSAGetLastError()
end if
wend
a = closesocket(sock)
call CloseWinsock
'INSERT FUNCTIONS HERE
'================================================
'=========COMMON FUNCTIONS=======================
'================================================
Function OpenUDPServSocket(Port)
HINTS.aiFlags.struct = 1
'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
CallDLL #kernel32, "RtlMoveMemory",_
sockaddrin as struct,_
sockaddr as struct,_
size as long,_
ret as void
print sockaddrin.sinPort.struct
'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 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$, bufSize, flags, byref sockaddr$, byref sockaddrSize)
buf$ = space$(bufSize)
struct a, b as long
a.b.struct = sockaddrSize
c$ = sockaddr$
CallDLL #ws2, "recvfrom",_
sock as ulong,_
buf$ as ptr,_
bufSize as long,_
flags as long,_
c$ as ptr,_
a as struct,_
recvfrom as long
sockaddr$ = c$
sockaddrSize = a.b.struct
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, 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 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, lpSockAddr$, namelen)
temp$ = lpSockAddr$
CallDLL #ws2, "bind",_
socket as ulong,_
temp$ as ptr,_
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 clisockaddr,_
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 cliaddrinfo,_
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]
struct clisockaddrin,_
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
Now I can send message to Liberty Basic.With END the program stops.
But how can I make something like: I send "left" than LB goes to chapter
will do something there and goes back to listen and same if I send "right" LB will go to chaper
.
Thanks