BBC BASIC
Programming >> Communication and Input/Output >> UDP support
http://bbcbasic.conforums.com/index.cgi?board=communication&action=display&num=1509445268

UDP support
Post by movr0r0 on Oct 31st, 2017, 10:21am

Hi all,

is there any kind of UDP support library in the lines of what SOCKLIB provides for TCP?

Many thanks in advance for your help,
Phil
Re: UDP support
Post by Richard Russell on Oct 31st, 2017, 5:53pm

on Oct 31st, 2017, 10:21am, movr0r0 wrote:
is there any kind of UDP support library in the lines of what SOCKLIB provides for TCP?

It's not difficult to use SOCKLIB in conjunction with some direct API calls to do UDP. My 'Free-dē' application communicates with the camera and (optionally) with the renderer using UDP and that works quite well. Here's some code from that application which you might find useful:

Code:
      REM Socket address structure:
      DIM SockAddr{sin_family{l&,h&}, sin_port{h&,l&}, sin_addr%, sin_zero&(7)}

      REM Open UDP socket:
      SYS `socket`, AF_INET, SOCK_DGRAM, IPPROTO_UDP TO UDPsocket%
      IF UDPsocket% = INVALID_SOCKET THEN
        SYS `WSAGetLastError` TO wsae%
        ERROR 72, "Couldn't open UDP socket (" + STR$(wsae%) + ")"
      ENDIF 

Then use SYS `recvfrom` and/or SYS `sendto` for the actual data transfer (I haven't listed that code because it's in assembler in my application, but it's straightforward).

Richard.
Re: UDP support
Post by movr0r0 on Nov 3rd, 2017, 12:16pm

Hi Richard,

Many thanks for your help, this is very useful;

However I am also looking to be able to use UDP listening sockets, hence, basically, my idea of, possibly, extend the SOCKLIB library -- and to stay as clean as possible with general BB4W and libraries organization. Any pros and/or cons?

Phil


Re: UDP support
Post by Richard Russell on Nov 3rd, 2017, 4:51pm

on Nov 3rd, 2017, 12:16pm, movr0r0 wrote:
Any pros and/or cons?

I've certainly got no objection to extending SOCKLIB to include UDP support. In fact the library is overdue for some attention, because it's one of the earliest to have been created and still contains 'legacy' code that is no longer required.

However I would want to be careful to keep the (much newer) BBCSDL version of socklib compatible with any changes to the BB4W version, which would mean taking into account how SDL_net implements UDP when designing the interface(s).

If you would like to do some preliminary work on extending the library, or even submit a new version for consideration, of course that's fine with me.

Richard.

Re: UDP support
Post by movr0r0 on Nov 29th, 2017, 4:48pm

Hi Richard,
sorry for this slow update;

So I have amended BB4W SOCKLIB library with a few simple things, mostly UDP client/server additions, as well as a few things concerning TCP -- taking care however to keep it fully backwards compatible with the stock version (and to use look-alike naming for added functions smiley.

Basically, additions are:
REM FN_tcplistenM(host$,port$,backlog) Create a TCP/IP listening socket (with backlog, i.e. allows multiple connections requests)
REM FN_checkconnectionM(socket) Check for a connection request and actually connects client (and leave listening socket open for more connections)
REM FN_udpsocketS(host$,port$) Create a UDP/IP service socket (explicit bind, server application)
REM FN_udpsocketC(host$,port$) Create a UDP/IP service socket (implicit bind, client application)
REM FN_readsocketfrom(skt,buf,len,sockaddrbuf,sockaddrbuflen) Read max 'len' bytes to addr 'buf' from 'skt', and get peer's details
REM FN_writesocketto(skt,buf,len,sockaddrbuf,sockaddrbuflen) Write max 'len' bytes at addr 'buf' to 'skt', specifying peer

I am not sure if you want me to send you the source code, or how to submit this new version. Let me know.

Ah, by the way, is there a better (shorter) way to get the base address of a structure than actually getting the address of its very first member?


Re: UDP support
Post by Richard Russell on Nov 29th, 2017, 6:47pm

on Nov 29th, 2017, 4:48pm, movr0r0 wrote:
I am not sure if you want me to send you the source code

Please do. As I mentioned before, SOCKLIB is one of the oldest libraries and contains 'obsolete' code so will need some attention before a new version could be released. I also want to keep the BBCSDL version of SOCKLIB 'in phase' with the BB4W version, so that implies the addition of compatible UDP functions to the former.

Quote:
is there a better (shorter) way to get the base address of a structure than actually getting the address of its very first member?

If I'm understanding the question, is there some reason why you're not simply using struct{} ?

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin2.html#structapi

Richard.

Re: UDP support
Post by movr0r0 on Dec 4th, 2017, 1:28pm

Hello Richard.

no, there is no (admittable) reason why I haven't used struct{} smiley

I'll email you the amended SOCKLIB.BBC library in a few minutes.

Cheers,
Phil.