Results 1 to 4 of 4

Thread: SwiftCommunication - UDP without struggle

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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
  •