PDA

View Full Version : SwiftCommunication - UDP without struggle



Petr Schreiber
04-05-2016, 23:28
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

ErosOlmi
06-05-2016, 07:15
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

Petr Schreiber
07-05-2016, 07:18
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

ErosOlmi
07-05-2016, 12:30
ahah, that was the trick you applied.
Good move.