' Creates a UDP server to monitor UDP broadcasts to port %UPort ' We use a UDP Server since we can nominate the specific port ' we wish to listen to: %UPort = 16001 #COMPILE EXE #DIM ALL #INCLUDE "g_win.inc" '"WIN32API.INC" #INCLUDE "C:\b\pb\gmt\g_kons.bi" #INCLUDE "C:\b\pb\gmt\g_type.bi" #INCLUDE "C:\b\pb\gmt\g_indep.bi" %Feedback = 101 DECLARE CALLBACK FUNCTION CBDialog DECLARE FUNCTION ServerThread(BYVAL hWnd AS LONG) AS LONG FUNCTION PBMAIN() AS LONG LOCAL hDlg AS LONG ' The dialog handle for the client DIALOG NEW 0, "UDP Server ",,, 220, 150, %WS_SYSMENU OR %WS_CAPTION TO hDlg CONTROL ADD LABEL, hDlg, %Feedback, "", 0, 0, 220, 130, %SS_SUNKEN CONTROL ADD BUTTON, hDlg, %IDCANCEL, "Close", 85, 133, 50, 14 DIALOG SHOW MODAL hDlg CALL CBDialog END FUNCTION CALLBACK FUNCTION CBDialog LOCAL idThread AS LONG SELECT CASE CBMSG CASE %WM_INITDIALOG THREAD CREATE ServerThread(CBHNDL) TO idThread THREAD CLOSE idThread TO idThread CASE %WM_COMMAND IF CBCTLMSG = %BN_CLICKED AND CBCTL = %IDCANCEL THEN DIALOG END CBHNDL, 0 CASE %WM_DESTROY FUNCTION = 1 END SELECT END FUNCTION FUNCTION ServerThread(BYVAL hWnd AS LONG) AS LONG LOCAL ip AS LONG ' This machines IP address LOCAL hUdp AS LONG ' UDP file number LOCAL Buffer AS STRING ' UDP data received LOCAL ipAddr AS LONG ' IP address of sending machine LOCAL ipPort AS LONG ' UDP Port of sending machine to reply to LOCAL x AS LONG ' Hold the size of the Dialog to test for closure LOCAL Op AS STRING ' Status text LOCAL servname AS STRING ' name of this PC LOCAL sendername AS STRING ' name of the PC sending to us ' Get this machines IP address HOST ADDR TO ip HOST NAME ip TO servname Op = "Starting UDP Server on " & MCASE$(servname) CONTROL SET TEXT hWnd, %Feedback, Op ' Open a specific UDP/IP port with a 60 second timeout hUdp = FREEFILE UDP OPEN PORT %UPort AS hUdp TIMEOUT 60000 IF ERR THEN BEEP DIALOG SEND hWnd, %WM_SYSCOMMAND, %SC_CLOSE, 0 EXIT FUNCTION END IF ' We opened the UDP/IP port Ok, so inform the user Op = Op & $CR & "Listening for broadcasts to " & DottedIP$(ip) & ":" & _ FORMAT$(%UPort) & "..." CONTROL SET TEXT hWnd, %Feedback, Op DO ' Start listening to the UDP/IP port ERRCLEAR UDP RECV #hUdp, FROM ipAddr, ipPort, Buffer ' Ignore any timout or other errors IF ERR THEN ITERATE HOST NAME ipAddr TO sendername ' We got one, so update the status screen Op = "Received from " & MCASE$(sendername) & DottedIP$(ipAddr) & ":" & _ FORMAT$(ipPort) & $DQ & Buffer & $DQ CONTROL SET TEXT hWnd, %Feedback, Op ' ' Reply with our time and update the screen ' UDP SEND #hUdp, AT ipAddr, ipPort, TIME$ ' Op = Op & $CR & "-> Replied with " & TIME$ ' CONTROL SET TEXT hWnd, %Feedback, Op ' Is main dialog still here? DIALOG GET SIZE hWnd TO x, x LOOP WHILE x CLOSE #hUdp END FUNCTION