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.
Asock.lsx is the C side of the interface, asock.plm is the Prolog side. You can load the LSX by adding:
lsxload=asock.lsx
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.
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.
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.
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.
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.
wsaStartup(VersionReqI, VersionV, HighVersionV, DescriptionStrV, StatusStrV, MaxSocketsV, MaxUdpDgV) wsaCleanup wsaGetLastError(ErrorNumV)
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.)
inet_addr(DotHostNameS, HostAddrV) inet_ntoa(HostAddrI, DotHostNameV)
These predicates get host, protocol and server names and address.
gethostbyaddr(HostAddrI, AliasListV, AddrListV) gethostbyname(HostNameS, AliasListV, AddrListV) gethostname(HostNameStrV) getprotobyname(ProtoS, ProtoNumV, AliasListV) getprotobynumber(ProtoI, ProtoNameV, AliasListV) getservbyname(ServNameS, ProtoNameS, ServPortNumV, ServAliasListV) getservbyport(PortnumI, ProtoNameS, ServNameV, ServAliasListV)
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.
accept(SocketI, RemoteHostAddrNumV, RemoteHostPortNumV, NewSocketNumV) bind(SocketI, LocalHostAddrI, LocalPortI) closesocket(SocketI) connect(SocketI, RemoteHostAddrI, RemoteHostPortI) getpeername(SocketI, RemoteHostAddrV, RemoteHostPortV) getsockname(SocketI, LocalHostAddrV, LocalHostPortV) getsockopt(SocketI, LevelI, OptionI, ValueNumV) ioctlsocket(SocketI, CmdI, ArgI, ValueV) listen(SocketI, BacklogI) recv(SocketI, DataStrV, DataLenI, FlagsI, ReceivedLenV) recvfrom(SocketI, DataStrV, DataLenI, FlagsI, FromAddrV, FromPortNumV, ReceivedLenV) select(ReadSocketsL, WriteSocketsL, ErrorSocketsL, WaitSecondsI, WaitThousSecsI, ReadSocketsResultListV, WriteSocketsResultListV, ErrorSocketsResultListV) send(SocketI, DataS, FlagsI, SentLenV) sendto(SocketI, DataS, FlagsI, ToAddrI, ToPortI, SentLenV) setsockopt(SocketI, LevelI, OptionI, ValueI) shutdown(SocketI, HowI) socket(SocketType, I, SocketProtocolI, SocketNumV)
Copyright ©1987-2011 Amzi! inc. All Rights Reserved. Amzi! is a registered trademark and Logic Server is a trademark of Amzi! inc.