Scripts/Socket System

From J Wiki
Jump to: navigation, search

This is a set of scripts to perform socket transfers. It is suitable for interprocess communication on a single machine or communication across a network, and supports both synchronous and asynchronous transfers.

File:Sockmux.ijs is the multiplexer for asynchronous sockets. Each asynchronous socket is created as its own object. send and recv events are called in the object when the socket becomes writable or readable. A time-checker entry point checks for timeouts; your program is expected call it from time to time.

File:Sockconnxactn.ijs handles transactions on connection-type (e. g. TCP) sockets. When you create a connxactn, it automatically creates a socket and performs the data transfer. By default the transfer consists of sending a string that you provide and reading the response until the other end closes the connection. Optionally, you may specify a delimiter for the response, either as a byte count or as a string, and in that case you will be notified when the delimiter is encountered. Transactions may be either synchronous, in which case all the data is transferred when the connxactn is created and the object returns with the (return code;data) that was received; or asynchronous, in which case the connxactn returns immediately and you must specify a callback, in the form of an x-argument and the name of a verb, and that verb will be called dyadically with the given x-argument and a y-argument of (return code;data), the call being made when the delimiter is encountered or the connection is closed.

File:Socklistener.ijs is used to implement servers. It listens for a connection and when it receives one, it creates an object to handle the connection. When you create the listener you give the name of the class used to handle connections.

File:Sockfileserver.ijs is an example of a server that uses the socklistener class. Connections to the server transfer files. The server is started by setting up a listener for it, as shown in File:Fileservmain.ijs which starts a fileserver and then exits.

File:Sockfileservclient.ijs is a client interface to the file server created by sockfileserver.ijs.

File:Dirmirror.ijs is an application that uses the file server to keep directories on different machines in sync.

File:Smtpemail.ijs is an application to send email to an SMTP server.

File:Pop3email.ijs is an application to receive email from a POP3 server.

File:Webio.ijs is an application to read web pages using HTTP. It has not been polished to releasable quality as the above have, but it works so I am including it.

These files make use of a few of my utilities from File:Langexten.ijs, File:Klutils.ijs, and File:Utils.ijs.

Example of reading a web page:

((<,<'') ; '' ; '/') webgetform 0 2 $ a:

Example of reading a web page asynchronously, with a callback on completion, and with operands for the GET:

(((,<'');('x op for callback');inthislocale 'google_callback') ; '' ; '/search') webgetform _2 ] 'source';'ig'

Henry Rich