tSocket Server/client question

tSocket Server/client question

Postby Marc Venken » Sun Mar 19, 2017 4:28 pm

Hello,

Looking into the Tsocket/client samples, i was thinking to find the solution to my problem.

A extern program generates a file, let say log.txt

I have a program that when I hit a button simply reads the file and do the next processes, so far ok.

Now I want a small FWH program that is listening (looking) at that location if that file excist. (After processing, I delete it and the 3th program generates it if needed)

The socket sample has the solution insite i think, but didn't get it to work. I can only activate the action from the sample client, so the action here has to be different.

Code: Select all  Expand view

function Server()

   oSocket = TSocket():New( 2000 )

   oSocket:bAccept = { | oSocket | oClient := TSocket():Accept( oSocket:nSocket ),;
                       oClient:Cargo := ST_COMMAND,;
                       oClient:bRead := { | oSocket | OnRead( oSocket ) },;
                       oClient:bClose := { | oSocket | OnClose( oSocket ) } }

   oSocket:Listen()

return nil

//------------------------------------------------------------------------//

function OnRead( oSocket )

   local cData := oSocket:GetData()
   local cToken

   LogFile( "sockserv.txt", { Len( cData ), cData } )

   if file("log.txt")
       msginfo("File found")
   endif


return nil

//------------------------------------------------------------------------//
 
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1338
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: tSocket Server/client question

Postby MarcoBoschi » Mon Mar 20, 2017 8:34 am

Mark,
I hope I understood
Compile it
Run it and clickon button
Then open a notepad and write something and save as log.txt
Every second (interval 1000) the program perform read_txt function ....

Bye

Code: Select all  Expand view
#include "fivewin.ch"
FUNCTION MAIN()
LOCAL oMain
LOCAL oTimer
LOCAL oButton
LOCAL oStringa , cStringa  := "Reading..."

DEFINE WINDOW oMain FROM 100 , 100 TO 500 , 500 PIXEL
DEFINE TIMER oTimer OF oMain INTERVAL 1000 ACTION ( cStringa := READ_TXT( oTimer ) , oStringa:settext( cStringa ) , oStringa:refresh() )

@ 1  , 1 BUTTON oButton  PROMPT "START POLLING" OF oMain ACTION oTimer:activate() SIZE 200 , 100
@ 10 , 1 SAY    oStringa PROMPT cStringa OF oMain
ACTIVATE WINDOW oMain

RELEASE TIMER oTimer
RETURN NIL

FUNCTION READ_TXT( oTimer )
LOCAL cStringa := ""
LOCAL cFile    := "log.txt"
oTimer:deactivate()

IF FILE( cFile )
   cStringa := MEMOREAD( cFile )
//    ? cStringa
   DO WHILE .T.
      IF FERASE( cFile ) = 0
         EXIT
      ENDIF
      sleep(1000)
   ENDDO
ENDIF
oTimer:activate()

RETURN cStringa
User avatar
MarcoBoschi
 
Posts: 1012
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: tSocket Server/client question

Postby Silvio.Falconi » Mon Mar 20, 2017 10:01 am

Marco,
is there a sample How send a txt message from client to client or client to server and viceversa ?
at school I have a simply lan IP4
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 6716
Joined: Thu Oct 18, 2012 7:17 pm

Re: tSocket Server/client question

Postby MarcoBoschi » Mon Mar 20, 2017 12:12 pm

SIlvio take a look in samples folder
sockcli.prg and sockserv.prg
in sockli.prg you find this function
Code: Select all  Expand view

function SendFile()

   local cFileName := cGetFile( "*.*", "Select a file to send by Internet" )

   if ! Empty( cFileName ) .and. File( cFileName )
      oSocket:SendData( "SENDFILE " + cFileName( cFileName ) )
      oSocket:SendFile( cFileName )
      MsgInfo( "File sent" )
   endif

return nil
 



and in sockserv.prg you find this code

Code: Select all  Expand view
     case oSocket:Cargo == ST_SENDFILE
           fwrite( oSocket:hFile, cData, Len( cData ) )
           LogFile( "sockserv.txt", { "writting..." } )
 
User avatar
MarcoBoschi
 
Posts: 1012
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: tSocket Server/client question

Postby Silvio.Falconi » Mon Mar 20, 2017 12:22 pm

si lo so ci ho provato tante volte ma non funziona volevo un esempietto da provare subito in loco
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 6716
Joined: Thu Oct 18, 2012 7:17 pm

Re: tSocket Server/client question

Postby MarcoBoschi » Mon Mar 20, 2017 12:35 pm

Silvio,
It works.
User avatar
MarcoBoschi
 
Posts: 1012
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: tSocket Server/client question

Postby Marc Venken » Mon Mar 20, 2017 10:38 pm

Marco,

The sample works great !! Thanks

There was also a recent Topic about Desktopalerts and so I copy/Pasted your code and the sample from 'Uwe' to one program.

Uwe, I hope you don't mind :?:

Now the program does :

Run in background (if HIDDEN is used)
Popup a alert with the data insite the file
(In my case the data comes from a PBX phone system with data of incoming call. Afther the call has ended, the PBX will erase the file and the program keeps listening until the next call to popup again.)
In the popup there is a action under the arror.
You can see the settings in the program.

Thanks to Marco and Uwe and the forum.

Working sample : (you could also check for a date/time and execute a Backup or update it to chat over the network. Many possibilities i think)

Code: Select all  Expand view
#include "fivewin.ch"
#include "Slider.ch"

STATIC nTransparency
STATIC c_path, c_path1

FUNCTION MAIN()
LOCAL oMain
LOCAL oTimer
LOCAL oButton
LOCAL oStr1

c_path  := cFilePath(GetModuleFileName( GetInstance() ) )
c_path1 := c_Path + "IMAGES\"


DEFINE WINDOW oMain FROM 100 , 100 TO 500 , 500 PIXEL
DEFINE TIMER oTimer OF oMain INTERVAL 1000 ACTION ( Listen( oTimer,oMain ) )

// say not needed
@ 4 , 5 SAY oStr1 PROMPT "
Create a log.txt file with some text inside" OF oMain
@ 5 , 5 SAY oStr1 PROMPT "
Copy this file into the program dir" OF oMain
@ 6 , 5 SAY oStr1 PROMPT "
The program sees this file and interact" OF oMain
@ 8 , 5 SAY oStr1 PROMPT "
Activate the program 'HIDDEN' to work in the background" OF oMain
@ 9 , 5 SAY oStr1 PROMPT "
This window is not needed anymore" OF oMain
@11 , 5 SAY oStr1 PROMPT "
I use this sample to popup my phone and show customer data" OF oMain

// use the HIDDEN version if you want to start the program and HIDE, so it's working in the background
//ACTIVATE WINDOW oMain HIDDEN ON INIT ( OTimer:Activate() )
ACTIVATE WINDOW oMain ON INIT ( OTimer:Activate() )

RELEASE TIMER oTimer
RETURN NIL

FUNCTION Listen( oTimer,oMain )
LOCAL cFile    := "
log.txt"
LOCAL cString  := "
"
oTimer:deactivate()

IF FILE( cFile )
   cString := MEMOREAD( cFile )

   DeskAlert(cString,oMain,10)  // 10 = seconds

   // In my case there is a external CTI program that will erase the file.
   // This DO/ENDDO can be erased than.
   DO WHILE .T.
      IF FERASE( cFile ) = 0
         EXIT
      ENDIF
      sleep(1000)
   ENDDO

ENDIF
oTimer:activate()

RETURN NIL

//----------------------------------------------------------------------------//
#define GWL_EXSTYLE   -20
#define WS_EX_LAYERED 524288
//----------------------------------------------------------------------------//

FUNCTION DESKALERT( cString, oWnd, nlongTimer )
local oDlg, oBrush, oFont
local hLogo := FWLogoBitMap()
local oBtnClose
local oBtnDown
local oFontBody
local lEnd := .T.
local oString


DEFINE FONT oFont NAME "
Verdana" BOLD
DEFINE FONT oFontBody NAME "
Verdana"  SIZE 0, -9
DEFINE BRUSH oBrush GRADIENT { { 1, nRgb( 221, 236, 253 ), nRgb( 95, 131, 179 ) } }

DEFINE DIALOG oDlg STYLE nOr( WS_POPUP, WS_BORDER ) ;
BRUSH oBrush SIZE 328, 73

@ 0.6, 6 SAY "
Incoming Call notification" OF oDlg TRANSPARENT FONT oFont

//@ 1.2, 6 SAY "
This a sample text area."+CRLF+"This a sample text area." OF oDlg TRANSPARENT FONT oFontBody SIZE 100,30

@ 1.2, 6 SAY oString VAR cString OF oDlg TRANSPARENT FONT oFontBody SIZE 100,30

@ 0.6, oDlg:nWidth-175 BTNBMP oBtnClose FILENAME c_path1 + "
Exit1.bmp" ;
SIZE 10, 10 OF oDlg NOBORDER ACTION ( lEnd := .F., oDlg:End() )

@ 0.6, oDlg:nWidth-185 BTNBMP oBtnDown FILENAME c_path1 + "
Drop.bmp" ;
SIZE 10, 10 OF oDlg NOBORDER ACTION ( nlongTimer := 0, ShowPopup( oDlg ) )

oBtnClose:ltransparent:=.t.
oBtnDown:ltransparent:=.t.

ACTIVATE DIALOG oDlg ;
ON INIT ( SetTransparent( oDlg ), oDlg:Shadow(), oDlg:Move( 70, 70, , , .T.) ) ;
ON CLICK oDlg:End() ;
ON PAINT DrawBitmap( hDC, hLogo, 15, 15 ) ;
VALID ( nlongTimer > 0, .T. ) ;
NOWAIT

IF nlongTimer > 0
  SYSWAIT1(nlongTimer)
  lEnd := .F.
  oDlg:End()
ENDIF

DeleteObject( hLogo )
oBrush:End()
oFont:End()
oWnd:SetFocus()

RETURN lEnd

//-------------------------------------------

STATIC FUNCTION SETTRANSPARENT( oDlg )

DEFAULT nTransparency:= 230  //  180

SetWindowLong( oDlg:hWnd, GWL_EXSTYLE, nOr( GetWindowLong( oDlg:hWnd, GWL_EXSTYLE ), WS_EX_LAYERED ) )

SetLayeredWindowAttributes( oDlg:hWnd, 0, nTransparency, 2 )

RETURN NIL

//-------------------------------------------

FUNCTION SHOWPOPUP( oDlg, nlongTimer )
local oPopSample

MENU oPopSample POPUP
  MENUITEM "
Open Customer dialogs" action xbrowse("customer")
  MENUITEM "
Send message on the network" action msginfo("Some data send and viewed by desktopalert")

  SEPARATOR
  MENUITEM "
Desktop Alert Settimgs" ACTION DeskTopAlertSettings(nlongTimer)
ENDMENU

ACTIVATE POPUP oPopSample OF oDlg AT 100, 100

RETURN NIL

//-------------------------------------------

FUNCTION SYSWAIT1( nLong )
local nSeconds

DEFAULT nLong := .1
nSeconds := Seconds() + nLong

WHILE Seconds() < nSeconds
  SysRefresh()
END

RETURN NIL

//------------------------------------------

STATIC FUNCTION DESKTOPALERTSETTINGS(nlongTimer)
Local oDlgSettings
Local cText_Duration:="
How long should the desktop alert appear ?"
local cText_transparency:="
How transparency should the desktop be ?"
local oTrans,oDuration
local obtn[3]
local oGrp[2]
LOCAL oSay[2]
local nOptionDuration := 60  // 25
local nOptionTrans:= 50    //180
Local nBottom   := 33
Local nRight    := 62
    *  Local nWidth :=  Max( nRight * DLG_CHARPIX_W, 180 )
    *  Local nHeight := nBottom * DLG_CHARPIX_H

DEFINE DIALOG oDlgSettings TITLE "
DeskTopAlert Settings" SIZE 450, 300 ;
STYLE nOr( WS_THICKFRAME, WS_SYSMENU )

@ 0, 2 GROUP oGrp[1] PROMPT "
Duration" OF oDlgSettings SIZE 200,40
@ 4, 2 GROUP oGrp[2] PROMPT "
Transparency" OF oDlgSettings SIZE 200,40
@ 0.5, 6 SAY oSay[1] PROMPT cText_Duration OF oDlgSettings   SIZE 120,10
@ 4.2, 6 SAY oSay[2] PROMPT cText_transparency OF oDlgSettings SIZE 120,10

@ 22, 45 SLIDER oDuration VAR nOptionDuration OF oDlgSettings ;
               HORIZONTAL ;
               RIGHT DIRECTION ;
               RANGE -5, 60 ;
               MARKS 11;
               EXACT;
               ON CHANGE  nlongTimer := nOptionDuration ;
               SIZE 102, 12 PIXEL

@ 78, 45 SLIDER oTrans VAR nOptionTrans OF oDlgSettings ;
               HORIZONTAL ;
               RIGHT DIRECTION ;
               RANGE -10, 200 ;
               MARKS 11;
               EXACT;
               ON CHANGE nTransparency := nOptionTrans ;
               SIZE 102, 12 PIXEL

@ 113, 14 BUTTON obtn[1] PROMPT "
&Preview" SIZE 45,12  OF oDlgSettings PIXEL
@ 113, 94 BUTTON obtn[2] PROMPT "
&Ok" SIZE 45,12  OF oDlgSettings PIXEL ;
ACTION  oDlgSettings:End(IDOK)

@ 113, 164 BUTTON obtn[3] PROMPT "
&Cancel" SIZE 45,12  OF oDlgSettings PIXEL ;
ACTION  oDlgSettings:End(IDCANCEL)

ACTIVATE DIALOG oDlgSettings CENTERED

IF oDlgSettings:nresult == IDOK
             msginfo(nTransparency, nlongTimer )
Endif

RETURN NIL
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1338
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 14 guests