Sockets LSX

The Sockets LSX implements the Sockets API for communicating via TCP/IP, notably over Internet. For Windows only the synchronous calls are implemented as the asynchronous calls do not make sense to use from Prolog.

This section assumes that you are very familiar with the Windows or Berkeley Sockets API and its use. If you'd like to learn more about sockets programming, we suggest "Developing for the Internet with Winsock" by Dave Roberts from Coriolis Group Books. There are also other texts as well as copious information on Internet.

Executables

Asock.lsx is the C side of the interface, asock.plm is the Prolog side. You can load the LSX by adding:

to your.cfg file or by calling AddLSX(). You can use the predicates from the listener by creating an amzi.cfg file with the above line, and by adding asock.plm as a library to your project.

Source Code

The source code for the Sockets LSX is provided in two primary files. Sockets.c is the C program that the LSX is built from. It provides all the extended predicates. asock.pro (asock_sol.pro for Solaris) is an 'include' file that provides a convenient means for using the predefined constants from Prolog.

Examples

There are two Prolog samples for the Sockets interface. Finger.pro implements a simple finger client. You specify a finger client to connect to and then provide a user-id, and it returns the 'finger' information for that user.

Sserver.pro and sclient.pro demonstrate a very simple server and its client. You need two machines running TCP/IP to run it. The server greets the client, accepts a command. If the command is 'shutdown' then the server exits, otherwise it waits for another command.

Extended Predicates

The Sockets LSX closely follows the Berkeley Sockets implementation with the following differences:

In addition, from Prolog, the following conventions are followed:

This means you do not have to do any conversions on these values if you pass exactly what you receive from one function to another.

The arguments to the predicates listed in the following sections are followed by a capital letter indicating the type as follows: I (Integer), L (List), S (String), and V (Variable).

Asock.pro (asock_sol.pro under Solaris) provides the definitions for the many constants used in the Sockets API. You can include it in your project, or consult asock.plm (or asock_sol.plm under Solaris) at runtime.

Startup/Shutdown Predicates

These predicates are used to start, stop and get error information from Windows sockets. These are the only 'wsa' routines that are implemented. The remainder of the extended predicates implement the standard (Berkeley) Sockets API.

Conversion Predicates

These predicates convert between the various data formats. (Note htonl, htons, ntohl, ntohs are not needed because network addresses are always kept in network byte order and protocol and port numbers are always kept in host byte order. The extended predicates internally do any conversions needed.)

Database Predicates

These predicates get host, protocol and server names and address.

Socket Predicates

These predicates create, manages and close sockets. Instead of passing the sockaddr structure, instead the relevant elements of that structure are passed and returned as separate arguments. These elements can be any combination of the address, port and/or socket numbers. And the values can be for either the remote host or the local host.

The select predicate uses a Prolog list of socket numbers instead of the fd_set structure. In practice, this makes it very easy to write recursive predicates to manage socket activity.

Copyright ©1987-2011 Amzi! inc. All Rights Reserved. Amzi! is a registered trademark and Logic Server is a trademark of Amzi! inc.