Page 1 of 1

Bidirectional communicaton between console app

Posted: Sat Jun 28, 2008 11:37 am
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.

Posted: Sat Jun 28, 2008 11:47 am
by blueznl
Wouldn't it be better to use mailslots?

http://www.purebasic.fr/english/viewtop ... 883#198883

Posted: Sat Jun 28, 2008 12:24 pm
by Fred
This way is fully cross-plateform, this is basically what we use for the IDE->Compiler communication.

Posted: Sat Jun 28, 2008 10:24 pm
by blueznl
Aaaaaah, okay. In that case: good stuff :-)