35
36:- module(sicstus_sockets,
37 [ socket/2, 38 socket_close/1, 39 socket_bind/2, 40 socket_connect/3, 41 socket_listen/2, 42 socket_accept/2, 43 socket_accept/3, 44 socket_select/5, 45 46 current_host/1, 47 hostname_address/2 48 ]). 49:- use_module(library(socket)). 50:- use_module(library(error)). 51:- use_module(library(apply)). 52:- use_module(library(pairs)). 53:- use_module(library(lists)). 54
55:- multifile sicstus:rename_module/2. 56
57sicstus:rename_module(sockets, sicstus_sockets).
65socket(Domain, Socket) :-
66 must_be(oneof(['AF_INET']), Domain),
67 tcp_socket(Socket).
68
69socket_close(Socket) :-
70 tcp_close_socket(Socket).
71
72socket_bind(Socket, Address) :-
73 ( Address = 'AF_INET'(Host, Port)
74 -> true
75 ; type_error(socket_address, Address)
76 ),
77 ( var(Host)
78 -> gethostname(Host)
79 ; true 80 ),
81 tcp_bind(Socket, Port).
82
83socket_connect(Socket, Address, StreamPair) :-
84 ( Address = 'AF_INET'(Host, Port)
85 -> true
86 ; type_error(socket_address, Address)
87 ),
88 tcp_connect(Socket, Host:Port),
89 tcp_open_socket(Socket, Read, Write),
90 stream_pair(StreamPair, Read, Write).
91
92socket_listen(Socket, Length) :-
93 tcp_listen(Socket, Length).
94
95socket_accept(Socket, Client, StreamPair) :-
96 tcp_accept(Socket, Socket2, Peer),
97 peer_to_client(Peer, Client),
98 tcp_open_socket(Socket2, Read, Write),
99 stream_pair(StreamPair, Read, Write).
100
101socket_accept(Socket, Stream) :-
102 socket_accept(Socket, _Client, Stream).
103
104
105peer_to_client(ip(A,B,C,D), Client) :-
106 Parts = [A,B,C,D],
107 ground(Parts), !,
108 atomic_list_concat(Parts, '.', Client).
109peer_to_client(ip(A,B,C,D), Client) :-
110 atomic_list_concat(Parts, '.', Client),
111 maplist(atom_number, Parts, Numbers),
112 length(Numbers, 4), !,
113 Numbers = [A,B,C,D].
114peer_to_client(_, Client) :-
115 domain_error(ip_address, Client).
137socket_select(TermsSockets, NewTermsStreams, SicsTimeOut, Streams, ReadStreams) :-
138 pairs_values(TermsSockets, Sockets),
139 append(Sockets, Streams, AllStream),
140 map_timeout(SicsTimeOut, TimeOut),
141 wait_for_input(AllStream, ReadyStream, TimeOut),
142 process_ready(ReadyStream, TermsSockets, NewTermsStreams, ReadStreams).
143
144map_timeout(off, infinite) :- !.
145map_timeout(S:U, Seconds) :- !,
146 Seconds is S+U/1000000.
147map_timeout(SicsTimeOut, _) :-
148 type_error(sicstus_timeout, SicsTimeOut).
149
150process_ready([], _, [], []).
151process_ready([H|T], TermsSockets, NewTermsStreams, ReadStreams) :-
152 memberchk(Term-H, TermsSockets), !,
153 socket_accept(H, Client, Stream),
154 NewTermsStreams = [Term-connection(Client,Stream)|NewTSTail],
155 process_ready(T, TermsSockets, NewTSTail, ReadStreams).
156process_ready([H|T], TermsSockets, NewTermsStreams, [H|ReadStreams]) :-
157 process_ready(T, TermsSockets, NewTermsStreams, ReadStreams).
164current_host(Host) :-
165 gethostname(Host).
171hostname_address(Host, Address) :-
172 nonvar(Host), !,
173 tcp_host_to_address(Host, IP),
174 peer_to_client(IP, Address)
SICStus 3-compatible
library(sockets)
.