Re: GNU Prolog - Java sockets communication

Dennis Decker Jensen <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Organization Gudrunsvej
Message-ID <[email protected]>
On Mon, 2004-10-25 at 16:43 +0100, Tiago Loureiro wrote:
> Greetz
> 
> I'm writting this because I'm really desperate trying
> to communicate from my JAVA client to a GNU PROLOG
> server.
> 
> I've used, for tests only, the code provided by Daniel
> Diaz on the thread "UDP sockets", for the PROLOG
> server:

Good example! Thanks for pointing to this.

> 
> server :-
>         socket('AF_INET', Sock),
>         socket_bind(Sock, 'AF_INET'(localhost, 7000)),
>         socket_listen(Sock, 4),
>         socket_accept(Sock, C, SI, SO),
>         close(SO),
>         format('accepting a connection from client :
> ~a~n', [C]),
>         repeat,
>         read(SI, T),
>         (   T \== end_of_file ->
>             write(T), nl,
>             fail
>         ;   !),   
>         close(SI),
>         socket_close(Sock).
> 
> 
> client_init :-
>         socket('AF_INET',Socket),
>        
> socket_connect(Socket,'AF_INET'(localhost,7000), SI,
> SO),
>         close(SI),
>         add_stream_alias(SO, out),
>         set_stream_buffering(out, line).
> 
> client_send(T):-
>         format(out, '~q.~n',[T]),
>         write(out).
> 

Notice the period (dot) in the format-term, right after ~q.

> client_term :-
>         close(out).
> 
> And my JAVA code is something like:
> 
> import java.net.*;
> import java.io.*;
> 
>     public class X{
> 
>         public static final String
> SERV_HOST="127.0.0.1";
>         public static final int SERV_PORT=7000;
> 
>         public static void main(String [] args){
>                
>             try{
>                 String s = "";
> 
>                 while (true){
>         
>                 Socket socket = new
> Socket(SERV_HOST,SERV_PORT);
>                 ObjectOutputStream oStream = new 
>                        
> ObjectOutputStream(socket.getOutputStream());  
>                 
>                 
>             BufferedReader in 
>                                 = new BufferedReader(
>                                         new
> InputStreamReader(System.in));
> 
>                         s = in.readLine();
>                                        
> oStream.writeObject(s);

You need a period (dot) after this too.
I cannot remember the Java API, but something like

o.print(".");

should do it.

>         
>                 socket.close();
>                 }
>             }
>         
>             catch (java.io.IOException ex){
>                 ex.printStackTrace();
> 
> (sorry for the code formatting from the mail client)
> 
> I understand that the problem is that the PROLOG
> server needs to get the socket stream input with a
> certain formating other than the one my JAVA client is
> providing.
> 
> My questions:
> 
> What is the best way for me to send data through the
> client?
> What sort of formating does it need to be accepted by
> the PROLOG server?

The server is trying to read prolog terms which need
to end with a period (dot), just like statements in
Smalltalk or Pascal need to end with a period (dot).
You can think of it as how you usually end your sentences
(in english anyway): With a period.

I don't know if it is the best way. I've been reading
the manual to find out if I can make gprolog read
and process other things than ordinary terms, that is something more
like ordinary strings and numbers that are not formatted like
terms, e.g. something like this:

# name amount comment
Dennis 100000 newbie from Denmark
Daniel 1000000 developer from France
Tiago 150000 newbie from England

which does not end with period but are more
akin to AWK-like line-records. I know it can,
just not how excactly -- still experimenting.

> Thank you for reading this newbie's request for help :)

(-: Thank you. I'm a newbie myself.

--
Dennis Decker Jensen

Behold, the fool saith, "Put not all thine eggs in the one
basket"--which is but a manner of saying, "Scatter your money and your
attention;" but the wise man saith, "Put all your eggs in the one basket
and--WATCH THAT BASKET."
                -- Mark Twain, "Pudd'nhead Wilson's Calendar"
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.