Announcement

Collapse
No announcement yet.

need PB/DLL UDP help!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • need PB/DLL UDP help!

    I want to make a simple UDP service in PB/DLL 6.0 to open a UDP
    port, send some data, and receive an acknowledgement on the same
    UDP port. I have the send working OK, but I cannot get the
    receive to work. Sorry, I cannot post my code right now, since
    I am not at my work computer.

    I have yet to find a real example of how to use UDP in PB/DLL.
    Didn't see any in my search of this forum.
    The disks came with examples for TCP, but none for using UDP. I
    would appreciate if any one who has this working with the UDP
    SEND, RECV, etc. commands post their working example. Thanks.

    Ivan Baggett


    ------------------

  • #2
    Here is a kind of "skeleton" code that might be helpful:
    Code:
    %UDPMsg = 3333		'just a value
    %defPortClient = 8001	'just a value
    %defPortServer = 8000 	'just a value      
    Global hDlgMain		AS LONG
    Global hi		AS LONG
    GLOBAL hUDPClient       AS LONG
    GLOBAL udpServerIP      AS STRING 
    FUNCTION PBMAIN ()      AS LONG
      #REGISTER NONE
    
      DIALOG NEW %NULL, "UDP Example", ,, 288, 200, %DS_3DLOOK OR %DS_CENTER OR %DS_MODALFRAME OR %WS_DLGFRAME, 0 TO hDlgMain
    
      DIALOG SHOW MODAL hDlgMain CALL DlgProc
    
    END FUNCTION 
    
    CALLBACK FUNCTION DlgProc
        DIM UDPMessage          AS STRING
        DIM ipvar&
        DIM pnumvar&   
    
        SELECT CASE CBMSG
    	CASE %WM_INITDIALOG
               'try to establish connections immediately
               EstablishConnection
         CASE %UDPMsg
            UDP RECV hUDPClient, FROM ipvar&, pnumvar&, UDPMessage
            IF UDPMessage > "" THEN
    	   'process the message
    	END IF     
        END SELECT        
    END FUNCTION
    
    FUNCTION EstablishConnection() AS LONG
        DIM	YourMessageToServer AS STRING
        ON ERROR GOTO ERRSub
        'establish a UDP
        hUDPClient = FREEFILE
        UDP OPEN PORT %defPortClient AS hUDPClient
        UDP NOTIFY hUDPClient, RECV TO hDlgMain AS %UDPMsg
        UDP SEND hUDPClient, AT FormatSTRIP(udpServerIP), %defPortServer, YourMessageToServer
        FUNCTION = %TRUE
    xERRSub:
        EXIT FUNCTION
    ERRSub:
        'no Internet connection
        hUDPClient = 0
        RESUME xERRSub
    END FUNCTION 
    
    FUNCTION FormatSTRIP(BYVAL sIP AS STRING) AS LONG
       DIM i AS LONG
       DIM a$
       REDIM nIp(1 TO 4) AS STRING
       REDIM xIp(1 TO 4) AS LONG
       REDIM tIp(1 TO 4) AS STRING
       nIp(1) = LEFT$(sIP, INSTR(sIP, ".") - 1)
       nIp(2) = MID$(sIP, LEN(nIp(1)) + 2, INSTR(LEN(nIp(1)) + 2, sIP, ".") - (LEN(nIp(1)) + 2))
       nIp(3) = MID$(sIP, LEN(nIp(1)) + LEN(nIp(2)) + 3, INSTR(LEN(nIp(1)) + LEN(nIp(2)) + 3, sIP, ".") - (LEN(nIp(1)) + LEN(nIp(2)) + 3))
       nIp(4) = MID$(sIP, LEN(nIp(1)) + LEN(nIp(2)) + LEN(nIp(3)) + 4)
       FOR i = 1 TO 4
            xIp(i) = VAL(nIp(i))
            tIp(i) = CHR$(xIp(i))
            a$ = a$ + tIp(i)
       NEXT
       FUNCTION = CVL(a$)
    END FUNCTION
    Regards,

    Peter Redei

    ------------------

    Comment


    • #3
      Thanks, Peter. Is it really necessary to do all that callback
      stuff? I was just wanting to do something simple, such as send
      some data, then wait for a response, then loop and do it again.
      Like this (not the actual source; I'm not at my work machine):

      fstuff = FREEFILE
      OPEN "FILE.DAT" FOR BINARY AS fstuff
      udpsock = FREEFILE
      UDP OPEN AS udpsock ' not in server mode
      WHILE NOT EOF
      GET$ fstuff, 1024, A$
      UDP SEND ' this line works
      UDP RECV ' wait for a reply, this doesn't work
      WEND

      Do you have to do the dialog callback stuff to make UDP work?
      They sure don't tell you that in the manual (what else is new?)

      Thanks.


      ------------------

      Comment

      Working...
      X