LB Booster
« Sending UDP data packets »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 03:55am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Sending UDP data packets  (Read 496 times)
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Sending UDP data packets
« Thread started on: Dec 30th, 2015, 1:07pm »

There is an ongoing discussion at the Liberty BASIC Yahoo! Group about sending UDP (User Datagram Protocol) packets from an LB program. This is a straightforward thing to do in LB or LBB code (sending UDP packets is probably the simplest of all socket transactions) yet, strangely, the majority of replies there have implied that it is difficult or impossible to achieve without an external 'helper' application!

I have listed below a program to demonstrate just how easy it is to send UDP packets from LB/LBB. For convenience the program goes through the entire startup, open socket, send packet, close socket, shutdown sequence for every packet sent. This should be OK so long as packets are being sent relatively infrequently, but in a program that sends large numbers of packets in quick succession it would be better to keep the socket open for the duration.

Of course I cannot reply to the LB Group myself, which is why I am posting the information here.

Richard.

Code:
    open "WS2_32" for DLL as #winsock
    
    packet$ = "Hello world!"
    ok = UDPsend("127.0.0.1", 12080, packet$)
    if ok then print "Packet sent" else print "UDPsend failed"
    
    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

    al = len(addr.struct)
    ml = len(msg$)
    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 

User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls