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
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