Communicate with other program (solved)

General FreeBASIC programming questions.
Post Reply
Provoni
Posts: 514
Joined: Jan 05, 2014 12:33
Location: Belgium

Communicate with other program (solved)

Post by Provoni »

Hey all,

What options are available to have a FreeBASIC program communicate/share information with another program written in another language or web service on Windows?

I am possibly looking to set up some kind of back end program that listens, processes requests and then send back useful information.

Thanks
Last edited by Provoni on Apr 10, 2022 10:07, edited 1 time in total.
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Communicate with other program

Post by SARG »

3 ways I think about :

- Simples files
- Memory-mapped files (https://docs.microsoft.com/en-us/window ... red-memory)
- Pipes

All have pros and cons.
DamageX
Posts: 130
Joined: Nov 21, 2009 8:42

Re: Communicate with other program

Post by DamageX »

another option: TCP/IP sockets
Provoni
Posts: 514
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Communicate with other program

Post by Provoni »

Thanks guys!

I like the memory-mapped files.

What are pipes?
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: Communicate with other program

Post by RockTheSchock »

I would go für TCP Sockets, which are especially fast for localhost connections on modern operating systems and also very flexible and reasonably easy to use.

Memory mapped files/ named pipes are handled a bit differently between Linux and Windows. Maybe there exists a library to facilitate the use or you would News to wrote an abstraction layer yourself If you warnt your Programm to bei Platform Independent.
Last edited by RockTheSchock on Apr 09, 2022 9:28, edited 2 times in total.
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Communicate with other program

Post by SARG »

Provoni wrote: Apr 09, 2022 5:27 What are pipes?
an external process' standard input (stdin) or output (stdout) stream for file operations.
Look at 'open pipe' in help file.

There is a drawback : "Bidirectional pipes are not supported by FB and must be implemented using the OS' API functions."
In fbdebugger I used the API functions, so I can give you some help if you choose this way.
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: Communicate with other program

Post by RockTheSchock »

Another drawback pipes can be slow on Windows (data rate).
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Communicate with other program

Post by marcov »

Memory mapping can also be used for uh, MEMORY. IOW a process is a server for a bunch of memory that the other machine can then read.

This is sometimes when services scale dynamically, to initialize a new process, or sometimes to persist in the event of a crash.

In a job we had a process (let's say process B) with a leaky/bad activex applet that we didn't have source for. A different Process (A) shared a bit of memory with the most important state variables and then started Process B.

Process B would read continue when it left off operating directly on the state of Process A's memory block. (or at least would write state changes to it at the earliest possible moment). It also occasionally increased a variable in the shared memory block.

If Process B crashed or became unresponsive (variable would not longer increment!), process A would terminate it if necessary and restart it. The such processes (A) are sometimes called "guardians"
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Communicate with other program

Post by grindstone »

Pipes are communication channels between programs. To see how they they work have a look at one of my "babies", the Colochessum chess engine testing and evaluating program. The pipe stuff is coded in the bipipe.bi library. To see how the procedures are used search them in the colochessum.bas source code. If you consider it too difficult to understand I could write for you some easier examples.
Provoni
Posts: 514
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Communicate with other program

Post by Provoni »

@marcov, that sounds reasonably complex, a cool example.

@grindstone, thank you for the code examples.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Communicate with other program (solved)

Post by badidea »

Here is an overview of various Inter-process communication methods on Wikipedia: https://en.wikipedia.org/wiki/Inter-pro ... munication
Of course, some are easier to implement (in freebasic) than others.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Communicate with other program (solved)

Post by TJF »

I use shared memory for inter process communication on LINUX. It's based on C-lib, so should also work on other platforms. Both apps have to run on the same OS.

Example including semaphores (= write protection, but it's faster let each app write to its own memory area, so no handshake is necessary)

The common shm.bi file

Code: Select all

#DEFINE IPC_CREAT  &o1000
#DEFINE IPC_EXCL   &o2000
#DEFINE IPC_NOWAIT &o4000
#DEFINE IPC_RMID   0

#DEFINE SEM_UNDO &h1000
#DEFINE GETPID   11
#DEFINE GETVAL   12
#DEFINE GETALL   13
#DEFINE GETNCNT  14
#DEFINE GETZCNT  15
#DEFINE SETVAL   16
#DEFINE SETALL   17

TYPE sembuf
  AS USHORT sem_num
  AS SHORT _
    sem_op _
  , sem_flg
END TYPE

EXTERN "C" LIB "c"
DECLARE FUNCTION nanosleep(BYVAL AS timespec PTR, BYVAL AS timespec PTR) AS LONG
DECLARE FUNCTION signal(BYVAL AS LONG, BYVAL AS SUB(BYVAL AS LONG)) AS LONG

DECLARE FUNCTION shmctl(BYVAL AS LONG, BYVAL AS LONG, BYVAL AS ANY PTR) AS LONG
DECLARE FUNCTION shmget(BYVAL AS key_t, BYVAL AS size_t, BYVAL AS LONG) AS LONG
DECLARE FUNCTION shmat(BYVAL AS LONG, BYVAL AS CONST ANY PTR, BYVAL AS LONG) AS ANY PTR
DECLARE FUNCTION shmdt(BYVAL AS CONST ANY PTR) AS LONG

DECLARE FUNCTION semctl(BYVAL AS LONG, BYVAL AS LONG, BYVAL AS LONG, ...) AS LONG
DECLARE FUNCTION semget(BYVAL AS key_t, BYVAL AS LONG, BYVAL AS LONG) AS LONG
DECLARE FUNCTION semop(BYVAL AS LONG, BYVAL AS ANY PTR, BYVAL AS size_t) AS LONG
END EXTERN
DIM SHARED AS ANY PTR SHM_PTR
DIM SHARED AS timespec SKIP
DIM SHARED AS LONG SEM_ID = -1, SHM_ID = -1

TYPE App1to2 ' App 1 writes, both can read
  AS BYTE dummy
END TYPE

TYPE App2to1 ' App 2 writes, both can read
  AS BYTE dummy
END TYPE

#MACRO SHM_CONF(_N_,_S_,_M_,_E_)
  SEM_ID = semget(&h12345678, _N_, _S_)
  IF -1 = SEM_ID                         THEN _E_ = "semget" : EXIT DO

  SHM_ID = shmget(&h12345677, SIZEOF(Status) + SIZEOF(Parameters), _M_)
  IF -1 = SHM_ID                         THEN _E_ = "shmget" : EXIT DO

  SHM_PTR = shmat(SHM_ID, 0, 0)
  IF SHM_PTR = CAST(ANY PTR, -1)         THEN _E_ = "shmat"  : EXIT DO
#ENDMACRO

#MACRO SHM_SERVER(_E_)
  SHM_CONF(2,(IPC_CREAT OR &o660), (IPC_CREAT OR IPC_EXCL OR &o660),_E_)
  IF -1 = semctl(SEM_ID, 0, SETVAL, 1) THEN _E_ = "semctl-0" : EXIT DO
  IF -1 = semctl(SEM_ID, 1, SETVAL, 1) THEN _E_ = "semctl-1" : EXIT DO
  A12 = NEW(SHM_PTR) Status
  A21 = NEW(SHM_PTR + SIZEOF(Status)) Parameters
#ENDMACRO

#MACRO SHM_CLIENT(_E_)
  SHM_CONF(2,&o660, &o660,_E_)
  A12 = CAST(App1to2 PTR, SHM_PTR)
  PAR = CAST(App2to1 PTR, A12 + 1)
#ENDMACRO

#MACRO SHM_CLIENT_CLOSE()
  IF -1 <> SHM_PTR THEN IF -1 = shmdt(SHM_PTR) THEN ?"shmdt" ELSE ?"shmdt OK"
  'IF -1 <> SEM_ID THEN IF -1 = semctl(SEM_ID, 0, IPC_RMID, 0) THEN ?"semctl" ELSE ?"semctl OK"
#ENDMACRO

DIM SHARED AS App1to2 PTR A12
DIM SHARED AS App2to1 PTR A21
The first starting app inits the SHM

Code: Select all

#INCLUDE ONCE "shm.bi"
VAR e = ""
SHM_SERVER(e)
IF LEN(e) THEN ?"failed: " & e : END(1)
...
SHM_CLOSE()
The following app connects to the SHM

Code: Select all

#INCLUDE ONCE "shm.bi"
VAR e = ""
SHM_CLIENT(e)
IF LEN(e) THEN ?"failed: " & e : END(1)
...
SHM_CLOSE()
ktuee753
Posts: 12
Joined: Mar 23, 2022 17:04

Re: Communicate with other program (solved)

Post by ktuee753 »

What about communicating using JSONs?
Post Reply