Bidirectional communicaton between console app

Share your advanced PureBasic knowledge/code with the community.
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Bidirectional communicaton between console app

Post by QuimV »

:)
Hi everyone,

I hope it will be useful to someone, here you have two small programs, master and slave, in order to implement simple bidirectional communications between two console applications.

Code: Select all

;
;master.pb
;QuimV - 2008
;
Global quit.l, progid.l
;
OpenConsole()
b.s = GetFilePart(ProgramFilename())
b.s = Left(b.s,Len(b.s)-1-Len(GetExtensionPart(ProgramFilename())))
ConsoleTitle(b.s)
EnableGraphicalConsole(0)
;
progid.l = RunProgram("Slave.exe","","",#PB_Program_Open|#PB_Program_Read|#PB_Program_Write)
;
While quit.l = 0
  Rx.s = Input()
  If IsProgram(progid.l) And ProgramRunning(progid.l)
    WriteProgramStringN(progid.l, Rx.s)
    If FindString(Rx.s,"EXIT",1)
      quit.l = 1
    EndIf
    Delay(50) ;Slave process the order
    If AvailableProgramOutput(progid.l) > 0
      PrintN(ReadProgramString(progid.l))
    EndIf 
  Else
    quit.l = 1
  EndIf
Wend
;
CloseConsole()
CloseProgram(progid.l)
;
End
--------------

Code: Select all

;
;slave.pb
;QuimV - 2008
;
Global quit.l
;
OpenConsole()
b.s = GetFilePart(ProgramFilename())
b.s = Left(b.s,Len(b.s)-1-Len(GetExtensionPart(ProgramFilename())))
ConsoleTitle(b.s)
EnableGraphicalConsole(0)
;
Procedure.s DOIT()
  ProcedureReturn "READY"
EndProcedure
;
While quit.l = 0
  ;Wait until the arrival of an order
  Rx.s = Input()
  ;Execute the order
  If FindString(Rx.s,"EXIT",1)
    quit.l = 1
  ElseIf FindString(Rx.s,"DOIT",1)
    ;Execute DOIT()
    a.s = DOIT()
    ;Return result
    PrintN("EXECUTED: DOIT() -> ANSWER: " + a.s)
  Else
    ;Retornar resultado
    PrintN("RECEIVED: " + Rx.s + " -> ANSWER: NO ACTION")
  EndIf
  ;
Wend
;
CloseConsole()
End
;
Greetings.
QuimV
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Wouldn't it be better to use mailslots?

http://www.purebasic.fr/english/viewtop ... 883#198883
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

This way is fully cross-plateform, this is basically what we use for the IDE->Compiler communication.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Aaaaaah, okay. In that case: good stuff :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply