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


Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.