Results 1 to 4 of 4

Thread: SwiftCommunication - UDP without struggle

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,155
    Rep Power
    736

    Lightbulb SwiftCommunication - UDP without struggle

    Hi,

    those who find UDP communication a bit mysterious might learn something by trying my simple wrappers.

    SwiftServer - type for handling servers
    SwiftClient - type for handling clients

    Full source code attached, you can launch one server and multiple clients, watching their chit chat

    Here examples of main application code, to demonstrate the ease of use:

    Server
    Uses "UI"
    
    #INCLUDE Once "SwiftServer.tbasicu"
    
    ' -- ID numbers of controls
    Begin ControlID
      %tbMessages
      %bClose  
    End ControlID    
    
    Begin Const
      %MAIN_WIDTH   = 640
      %MAIN_HEIGHT  = 240
    End Const
    
    Function TBMain()
      UInt32 hDlg
    
      Dialog New Pixels, 0, "Server",-1,-1, %MAIN_WIDTH, %MAIN_HEIGHT, %WS_POPUP | %WS_VISIBLE | %WS_CAPTION | %WS_SYSMENU | %WS_MINIMIZEBOX To hDlg
      
      Control Add Textbox, hDlg, %tbMessages, "", 5, 5, %MAIN_WIDTH-10, %MAIN_HEIGHT-40, %ES_AUTOHSCROLL | %ES_AUTOVSCROLL | %ES_LEFT | %WS_BORDER | %WS_TABSTOP | %ES_MULTILINE  
      Control Add Button, Name "CloseButton", hDlg, %bClose, "Click to close", %MAIN_WIDTH-105, %MAIN_HEIGHT-30, 100, 25
     
      Dialog Show Modal hDlg, Call cbDialog
    
    End Function
    
    ' -- Callback for dialog
    CallBack Function cbDialog()
    
      Select Case CBMSG
    
        Case %WM_INITDIALOG
          Global server As SwiftServer(CBHNDL, 5000)
    
        Case server.MessageEvent
          If server.CanHandleMessage(CBLPARAM) Then
            String message = Trim$(server.TryReceive())+$CRLF 
            If Len(message) Then
              Control Appendtotop Text CBHNDL, %tbMessages, server.GetClientIP + ":" + server.GetClientPort + " " + message
              server.TryReply("ACK")
            End If  
          End If
        
      End Select
    
    End Function 
    
    CallBack Function CloseButton_OnClick()
      Dialog End CBHNDL
    End Function
    
    Client
    Uses "Console"
    
    #INCLUDE Once "SwiftClient.tbasicu"
    
    Function TBMain()
      
      Dim c As SwiftClient(5001, 5010)
      c.SetServer(INET_IpToNumber("127.0.0.1"), 5000)
      Console_SetTitle("Client running at " + c.Port)
      
      String reply
      Long counter
      
      c.TrySend("Hi, I am client")
      Do
        reply = c.TryReceive()
        If Len(reply) = 0 Then Exit Do
      
        Incr counter
        c.TrySend("Ciao signore #" + counter)
        Print "."
        Sleep 1000
      
      Loop
      
      PrintL "Server did not ACK :'("
      PrintL "Press any key to quit..."
      WaitKey
    End Function
    

    Petr
    Attached Files Attached Files
    Last edited by Petr Schreiber; 04-05-2016 at 23:30.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,818
    Rep Power
    10
    Great Petr,

    it can be the base for many kind of situations where programmer needs to communicate between applications.
    Maybe some auto-discovery functions are needed: client should broadcast a special message in order to discover if the is a server. Server should intercept broadcast message and reply that it is alive. Client then knows the server ip

    Ciao
    Eros
    Last edited by ErosOlmi; 06-05-2016 at 07:18.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,155
    Rep Power
    736
    Hi Eros,

    this could be nice enhancement!

    I use the solution for multiple clients reporting number of processes, cpu load, memory and free space to the main application.

    There is no server discovery, config is INI driven.

    Each client has own INI file with client related stuff + one path in that client INI that points to another INI, at network location, which contains Server config.
    This way I can change the central Server config INI, and all clients know about new server IP / port.


    Petr
    Last edited by Petr Schreiber; 07-05-2016 at 13:15.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,818
    Rep Power
    10
    ahah, that was the trick you applied.
    Good move.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •