Liberty BASIC Community Forum
« UDP comunications »

Welcome Guest. Please Login or Register.
Feb 19th, 2018, 12:00am


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: UDP comunications  (Read 545 times)
papa3110
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 3
xx UDP comunications
« Thread started on: Jun 3rd, 2016, 11:29am »

I'm trying to use the SimpleSocket dll with liberty basic.
I have VB code but having a hard time translating code.
Here is the VB

Receiver

Set socket=WScript.CreateObject("SimpleSocket.Udp", "event_")
Sub event_OnReceive(data)
WScript.Echo "Received: " & data
End Sub

socket.Listen "127.0.0.1", 10000
While(True)
WScript.Sleep(5000)
Wend
socket.Abort

Sender

Set socket=WScript.CreateObject("SimpleSocket.Udp")
socket.Send "127.0.0.1", 10000, "Data sent from the client"

Could anyone show me what I need for LB?

Thanks smiley
User IP Logged

Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2301
xx Re: UDP comunications
« Reply #1 on: Jun 4th, 2016, 3:42pm »

I'll look for some sample code I have that shows how to use UDP communications.


It is, unfortunately, fairly complex code, though. We have simpler to use libraries for TCP use(See Mesock32, I believe), but I don't think it supported UDP.
User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
papa3110
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 3
xx Re: UDP comunications
« Reply #2 on: Jun 6th, 2016, 2:09pm »

I do have code using WS2_32.dll that works with Liberty Basic for sending UDP and it works great

open "WS2_32" for DLL as #winsock

packet$ = ":33,b"+chr$(10)+chr$(13)
ok = UDPsend("172.16.16.51",1001, packet$)
if ok then print "Packet sent" else print "UDPsend failed"
print packet$
close #winsock
end

function UDPsend(addr$, port, msg$)
AF.INET = 2
SOCK.DGRAM = 2
IPPROTO.UDP = 17
UDPsend = 0

port = (port and hexdec("FF00")) / 256 + (port and 255) * 256 ' swap bytes
calldll #winsock, "inet_addr", addr$ as ptr, ipaddr as ulong

struct WSAdata, d as char[398]
calldll #winsock, "WSAStartup", 514 as long, WSAdata as struct, ret as long
if ret then exit function

calldll #winsock, "socket", AF.INET as long, SOCK.DGRAM as long, _
IPPROTO.UDP as long, socket as long
if socket = -1 then exit function

struct addr, family as short, port as short, addr as ulong, zero as char[8]
addr.family.struct = AF.INET
addr.port.struct = port
addr.addr.struct = ipaddr
print port
print AF.INET
print ipaddr


al = len(addr.struct)
ml = len(msg$)
print ipaddr
print ml

calldll #winsock, "sendto", socket as long, msg$ as ptr, ml as long, _
0 as long, addr as struct, al as long, sent as long

calldll #winsock, "closesocket", socket as long, ret as long
calldll #winsock, "WSACleanup", ret as long

if sent = ml then UDPsend = 1
end function


maybe that would be the better dll to use?
User IP Logged

Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2301
xx Re: UDP comunications
« Reply #3 on: Jun 6th, 2016, 10:18pm »

That's fairly certain to the code I have lying around. (Sorry, it's been a busy week. Preparing for a trip to Seattle)

WS2_32 is the Winsock library, which is the official way to do network programming in Win32.
« Last Edit: Jun 6th, 2016, 10:22pm by Chris Iverson » User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
Konijn
New Member
Image


member is offline

Avatar




PM


Posts: 17
xx Re: UDP comunications
« Reply #4 on: Dec 7th, 2017, 10:14am »

Works great, I want to use it to send data to an arduino.

Is it possible to skip the popup screen when you send something?
User IP Logged

Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2301
xx Re: UDP comunications
« Reply #5 on: Dec 7th, 2017, 11:22am »

What popup screen are you speaking of? I don't see anything in here that would be popping something up.
User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
Konijn
New Member
Image


member is offline

Avatar




PM


Posts: 17
xx Re: UDP comunications
« Reply #6 on: Dec 7th, 2017, 2:09pm »

Hello Chris,

I see it, I was doing it wrong. I am sorry. I was copying this code in to my code and that goes wrong. rolleyes

Thanks Jack
User IP Logged

Konijn
New Member
Image


member is offline

Avatar




PM


Posts: 17
xx Re: UDP comunications
« Reply #7 on: Jan 9th, 2018, 11:12am »

Is there also a possibillity to receive UDP packets?

Thanks!
User IP Logged

Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2301
xx Re: UDP comunications
« Reply #8 on: Jan 18th, 2018, 12:24pm »

Sure. I know I have code around that uses UDP.
User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
Konijn
New Member
Image


member is offline

Avatar




PM


Posts: 17
xx Re: UDP comunications
« Reply #9 on: Jan 20th, 2018, 11:35am »

Okee Chris, thats great. I am working in a project with cow recognizing in milkstable by transponder. It seems now that a arduino will send (UPD) to the computer (Liberty Basic) the transpodercodes. I am not that far yet that I have details. That will come this spring.
I am able to send UDP what works great with above code.
Is it a difficult code to receive?

Jack
User IP Logged

Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2301
xx Re: UDP comunications
« Reply #10 on: Jan 20th, 2018, 7:25pm »

Receiving isn't too difficult, the only problem is that your program hangs while waiting for something to come in. You can't do any other processing while waiting for input. Whether that's good or not depends on what you intend to do.
« Last Edit: Jan 20th, 2018, 7:25pm by Chris Iverson » User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
Konijn
New Member
Image


member is offline

Avatar




PM


Posts: 17
xx Re: UDP comunications
« Reply #11 on: Jan 21st, 2018, 2:04pm »

No I think thats no problem, otherwise I have to do it by rs2232 and a comport. What I use now. Program is now also waiting for input.
Do you have an example off the code?
Best regards,
Jack
User IP Logged

Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2301
xx Re: UDP comunications
« Reply #12 on: Jan 22nd, 2018, 10:02am »

Yeah, I'm putting it together, I just want to make sure the old code I have still works before posting it.
User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
papa3110
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 3
xx Re: UDP comunications
« Reply #13 on: Jan 22nd, 2018, 11:19am »

Great I still haven't finished the project I needed the UDP comms for


Thanks smiley
User IP Logged

Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2301
xx Re: UDP comunications
« Reply #14 on: Jan 24th, 2018, 10:24am »

Ok, I believe this demo should work. It's working on my machine, at least.


There are two examples here, a corresponding server and client for testing. At the end of this post is a common set of functions that will need to be copied into BOTH programs before you'll be able to run either one.

Server:
Code:
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 


Client:
Code:
gosub [structInit]

Call OpenWinsock
hostName$ = "localhost"  'name or IP address
port = 27015
sock = OpenUDPClientSocket(hostName$, port)

sendMsg$ = "END"
a = send(sock, sendMsg$, len(sendMsg$))
if a = -1 then
    print "Send error - ";WSAGetLastError()
end if

print a
buf$ = space$(1024)
structLen = len(sockaddrin.struct)
sockaddr$ = space$(structLen)
ret = recvfrom(sock, buf$, 1024, 0, sockaddr$, structLen)
If ret = -1 then
    print "recv error - ";WSAGetLastError()
Else
    print left$(buf$, ret)
End If

call freeaddrinfo
a = closesocket(sock)
call CloseWinsock

'INSERT FUNCTIONS HERE 


Common functions:

Code:
'================================================
'=========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 


EDIT: To fix miscopied server code.
« Last Edit: Jan 28th, 2018, 4:56pm by Chris Iverson » User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls