Web service – Socket – TCPIP [examples]

Open a TCP/IP socket
FSOCKOPEN
fp=fsockopen("www.server.com",80)
if fp > 0
    ...
endif
fclose(fp)

https://www.lianja.com/doc/index.php/FSOCKOPEN()


Read data from an open socket connection
SOCKET_READ
// read the JSON encoded object from the socket
data = socket_read(skt)
 
// assume we are connected to node.js (as an example) 
// and decode the JSON string
myobj = json_decode(data)
 
// now myobj is a VFP object with all the properties 
// (and subobjects/arrays etc)

https://www.lianja.com/doc/index.php/SOCKET_READ()


Write data to an open socket connection
SOCKET_WRITE
// assume we are connected to node.js (as an example) then 
// send the data in JSON format
data = json_encode(myobj)
 
// write the JSON encoded object to the socket
m_count = socket_write(skt, data)
if m_count != len(data)
    // error sending data
endif

https://www.lianja.com/doc/index.php/SOCKET_WRITE()


Create a socket server listening on a port
SOCKET_SERVER
proc myhandler(skt as numeric)
    private m_running = .t.
    do while m_running
        if socket_peek(skt) <= 0
            sleep 1
            loop
        endif
        data = socket_read(skt)
        // do what you want to do with the data
    enddo
endproc
 
// create a server listening on port 9000
skt = socket_server(9000, "myhandler")
if skt < 0
    // error
endif

https://www.lianja.com/doc/index.php/SOCKET_SERVER()


Open a socket
SOCKET_OPEN
// connect to a remote server e.g. node.js listening on port 9000
skt = socket_open("mydomain.com", 9000)
if skt < 0
    // error
endif

https://www.lianja.com/doc/index.php/SOCKET_OPEN()


Close an open socket connection
SOCKET_CLOSE
// close the socket connection
m_skt = socket_close(skt)
if m_skt != skt
    // error closing socket
endif

https://www.lianja.com/doc/index.php/SOCKET_CLOSE()


Error number of the last socket operation error
SOCKET_LASTERROR
// check the socket operation
if socket_lasterror() != 0
    // remote server connection failed, try reconnecting
endif

https://www.lianja.com/doc/index.php/SOCKET_LASTERROR()


Check if there is data available on an open socket connection
SOCKET_PEEK

 

// check the socket connection
if socket_peek(skt) > 0 
    // we have some data to read...
endif

https://www.lianja.com/doc/index.php/SOCKET_PEEK()


Socket_server()

proc myhandler(skt as numeric)
    private m_running = .t.
    do while m_running
        if socket_peek(skt) <= 0
            sleep 1
            loop
        endif
        data = socket_read(skt)
        // do what you want to do with the data
    enddo
endproc

// create a server listening on port 9000
skt = socket_server(9000, "myhandler")
if skt < 0
    // error
endif

Socket_open()

// connect to a remote server e.g. node.js listening on port 9000
skt = socket_open("mydomain.com", 9000)
if skt < 0
    // error
endif

socket_peek()

if socket_peek(skt) > 0 
    // we have some data to read...
endif

socket_read()

// read the JSON encoded object from the socket
data = socket_read(skt)

// assume we are connected to node.js (as an example) 
// and decode the JSON string
myobj = json_decode(data)

// now myobj is a VFP object with all the properties 
// (and subobjects/arrays etc)

socket_write()

// assume we are connected to node.js (as an example) then 
// send the data in JSON format
data = json_encode(myobj)

// write the JSON encoded object to the socket
m_count = socket_write(skt, data)
if m_count != len(data)
    // error sending data
endif

socket_close()

// close the socket connection
m_skt = socket_close(skt)
if m_skt != skt
    // error closing socket
endif
socket_lasterror()	None	
// check the socket operation
if socket_lasterror() != 0
    // remote server connection failed, try reconnecting
endif

https://www.lianja.com/doc/index.php/Working_with_Sockets_and_Web_Services


Web services

Lianja 3.4 provides cross-platform consumer/producer message processing functions which can be used very effectively with web services.

These functions provide cross-platform messaging enabling you to offload slow processing e.g. send email, to a background service.

On Windows the messaging functions are implemented using named pipes and on Linux using POSIX message queues.

Firstly you create your “consumer” and run it as a scheduled task in Windows or a cron job in Linux:

Code:
local mqdes, msg, data

// Create the consumer and wait for a connection
mqdes = mqCreate(“unique_name_of_your_consumer”)

// loop processing messages as they come in from multiple producers
do while .t.
       msg = mqReceive(mqdes)
        if len(msg)=0
            loop
        endif
        data = json_decode(msg)
        // Now process the message which is now encoded as an object
        // …
enddo

Now the producer(s) code looks like this:

Code:
// Create the consumer and wait for a connection
mqdes = mqOpen(“unique_name_of_your_consumer”)
msg = json_encode(anobject)
mqSend(mqdes, msg)
mqClose(mqdes)

Alternatively you can combine all the above code into a shortened version:

Code:
mqSendMessage(“unique_name_of_your_consumer”,  json_encode(anobject))

Q:

In a JavaScript application, I am trying to add a blank grid and then populate it later from an array using additems().

I know this is not yet supported in custom sections, but I thought it may be possible in a standard section.

A:

https://www.lianja.com/community/sho…7702#post17702

Custom grids which can be loaded from an external web service are now implemented in 4.0.1