Working with UDP is somewhat different as it is connectionless, which apart from meaning delivery is not guaranteed means you just can’t send a reply like the TCP server can. However it does give you the IP address of the sending computer so if you want to send information back then you just do a UDP Send. To illustrate here are some fragments from a private network DNS server I wrote a few years ago.
Code:
‘ union for converting long IP to dotted IP UNION LtoB l AS LONG b(3) AS BYTE END UNION ‘ set up the window to receive messages DIALOG SHOW MODELESS hdlg, CALL GotUdt 'create a window to receive messages UDP OPEN PORT 13000 AS #1 'open a udp port to receive registrations UDP NOTIFY #1, RECV TO hdlg AS 888 'and have it notify when a message is received ‘ receive a message and reset for next CALLBACK FUNCTION GotUdt() SELECT CASE CBMSG CASE 888 UDP RECV #1, FROM ip.l, pnumvar&, MyName$ 'receive a registration 'and extract the senders IP from the packet header and convert to dotted notation adding to registration string NewText$ = MyName$ + " " + FORMAT$(ip.b(0)) + "." + FORMAT$(ip.b(1)) + "." + FORMAT$(ip.b(2)) + "." + FORMAT$(ip.b(3)) y& = INSTR(MyName$," on ") t$ = LEFT$(MyName$, y& - 1) 'extract the name from the string which also contains date etc l& = LEN(t$) FOR y& = 0 TO ccnt - 1 IF LEFT$(comps(y&),l&) = t$ THEN EXIT FOR 'check if name already registered 'note this test is case sensitive as it assumes you will never change the name 'being transmitted by the registration program NEXT IF y& < ccnt THEN comps(y&) = newtext$ 'yes so just replace current reg with new ELSE comps(ccnt) = newtext$ 'no so create a new registration ccnt = ccnt + 1 REDIM PRESERVE comps(ccnt) END IF LISTBOX RESET hdlg, 1 FOR y& = 0 TO ccnt - 1 LISTBOX ADD hdlg, 1, comps(y&) NEXT UDP CLOSE #1 SLEEP 500 UDP OPEN PORT 13000 AS #1 UDP NOTIFY #1, RECV TO hdlg AS 888 ‘*********** the sending program just sends it data at a regular period UDP OPEN AS #1 EndlessLoop: Stext$ = compnme$ + " on " + DATE$ + " at " + TIME$ “the sending IP number is by the name server 'when it receives the packet UDP SEND #1, AT ip.l, 13000, stext$ 'note ip.l is a long not dotted format 'I use port 13000, you can choose any port number that is not already used on the 'computer running the name server, of course you need to set your name server 'to receive on that port SLEEP tme& GOTO EndlessLoop UDP CLOSE #1 ‘ *******************
Leave a comment: