How test if an UDP client is alway online ?

Archives Forums/BlitzPlus Programming/How test if an UDP client is alway online ?

Filax(Posted 2004) [#1]
How test if an UDP client is alway online ? an idea ?


Wayne(Posted 2004) [#2]
One possible way:

Have the client send out and ACK every 2 seconds, and have the server show client online if the ACK is received.

Once server shows client online and doesn't get ACK within four seconds show Client NOT online. Adjust times to suit your purpose.

B3D Sample code:

format_code('
;
; Sample UDP communications program (Client/Server same machine)
; --- Now with client heartbeat ---

; Author: W.Gray Last updated: 1/5/4
; Comment: Free code no restrictions, use at your own risk.
;
; Instructions:
; Start program, select mode, your IP will be displayed, enter destination IP , type.

; Testing client and server on same machine, run server with 127.0.0.1, and run client with 127.0.0.1


Print "Sample UDP Communications"

; Ask for mode

Print "1-Server"
Print "2-Client"
Mode$=Input$(">")

Select Mode$
Case "1"
Udp_Port=4000 ; I/O Port
Dest_Port=4001
AppTitle "Server on UDP Port "+Str(Udp_Port)

Case "2"
Udp_Port=4001 ; I/O Port
Dest_Port=4000
AppTitle "Client on UDP Port "+Str(Udp_Port)

Default
Udp_Port=4000 ; I/O Port
Dest_Port=4001
AppTitle "Server on UDP Port "+Str(Udp_Port)
End Select


; Display all local user IP's
;
Print "Local IPs:"
For i=1 To CountHostIPs(GetEnv("localhost"))
Print DottedIP(HostIP(i))
Next

Print "Enter Destination IP#: xxx.xxx.xxx.xxx"
Dest_IP$=Input$(">")

; Convert to integer IP
IP= Int_IP(Dest_IP$)

Print
Print "Starting Chat <esc> to quit"

; Allocate ports, get stream id's
udp_stream=CreateUDPStream(Udp_Port) ; Read Stream


.loop
t1=t0
t0=MilliSecs()

Elapsed_Time=Elapsed_Time+(t0-t1)

; Poll Keyboard for Keypress.
byte1=GetKey()

; transmit each key press.
If byte1>0 Then
; If 'Enter' pressed force new line locally, and send it.
If Byte1=13 Then
Print
; put character in stream buffer.
WriteByte(udp_stream,byte1)
; send all characters in the stream buffer.
SendUDPMsg udp_stream,IP,Dest_Port
Else
; Show character before we send it.
Write Chr(Byte1)
; put character in stream buffer.
WriteByte(udp_stream,byte1)
; send all characters in the stream buffer.
SendUDPMsg udp_stream,IP,Dest_Port
EndIf
EndIf

;Client Sends Heart Beat every 2 sec (2000ms)
If Elapsed_Time>2000 And mode$="2" Then
; Ack Character
Byte1=6
; put character in stream buffer.
WriteByte(udp_stream,byte1)
; send all characters in the stream buffer.
SendUDPMsg udp_stream,IP,Dest_Port
; reset time
Elapsed_Time=0
EndIf

; Load UDP message into stream
IP_rd=RecvUDPMsg(udp_stream)

; Test for messages in stream
If IP_rd <> 0 Then

;Get number of bytes in UDP stream
Buflen=ReadAvail(udp_stream)
If Buflen > 0 Then

; Get received character and print to display
byte1=ReadByte(udp_stream)
If byte1=6 Then
If online=False Then
AppTitle "Server on UDP Port "+Str(Udp_Port)+" Client Online"
online=True
EndIf
Elapsed_Time=0
Else
; Translate <enter> key
If byte1=13 Then
Print
Else
Write Chr$(byte1)
EndIf
EndIf
EndIf

EndIf

; Look for <escape>
If KeyDown(1) Then
Goto quit
EndIf

; Show client NOT online if no ack within 4 sec
If Elapsed_Time>4000 And mode$="1" And online=True Then
AppTitle "Server on UDP Port "+Str(Udp_Port)
online=False
Elapsed_Time=0
EndIf

Goto loop

; Cleanup and terminate
.quit
CloseUDPStream udp_stream



;--------------------------------------
; FUNCTION: INT_IP(IP$)
;
; Returns integer value of a IP Address string
; IP$ Format n.n.n.n

Function Int_IP(IP$)
a1=val(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
a2=val(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
a3=val(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
a4=val(IP$)
Return (a1 Shl 24) + (a2 Shl 16) + (a3 Shl 8 ) +a4
End Function


;--------------------------------------
; FUNCTION: VAL(STRING$)
;
; Returns integer value of a string

Function val(String$)
ac=String$
Return String$
End Function
')


Filax(Posted 2004) [#3]
Thanks very much for this really good example !


Wayne(Posted 2004) [#4]
np, your welcome.