users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Socket communication between GNU prolog and Java.


From: Vic Bancroft
Subject: Re: Socket communication between GNU prolog and Java.
Date: Sun, 23 Mar 2003 23:20:22 -0500 (EST)

On Fri, 21 Mar 2003 address@hidden wrote:

> I attempted to follow more strictly the format of the CLPGUI program (...)
> When I run the server, and then the client, I get the following error: ...

Interesting, could you share a URL for the CLPGUI program ?  Here is a version
of your program that seems to work nicely with a telnet client.  It will repeat
accepting terms from the connected socket until exit, halt or quit is read.  
For debugging purposes, it is inconvenient to map the standard input and output
streams to the socket.  Once the server does more than echo terms, perhaps like
call/1, then redirecting standard output to the socket stream may be needed.  
Also, one can get a tad more control over output using format/2 and format/3.

  %%% -*- Mode: Prolog; -*-
  
  gserver( Port ) :-
      catch( accept_connection( Port, N, I, O ), 
           X, fail_message( X, N ) ),
      catch( repeat_read( N, I, O ),
           E, fail_message( E, N ) ),
      close_connection( N, I, O ).
  
  accept_connection( Port, N, I, O ) :-
      socket('AF_INET', N ),
      socket_bind( N, 'AF_INET'( Hostname, Port ) ),
      format(  "~w : ~w ~n", ['Hostname', Hostname ] ),
      socket_listen( N, 5 ),
      socket_accept( N, I, O ).
  
  repeat_read( N, I, O ) :-
      repeat,
      \+ read_connection( N, I, O ).
  
  read_connection( N, I, O ) :-
      format_prompt( O ),
      catch( read( I, Term ), 
           E, fail_message( E, N ) ),
      format( "~w ~n", [Term] ),
      format( O, "~w ~n", [Term] ),
      \+ quit_connection( Term ).    
  
  format_prompt( O ) :-
      format( O, "~w ", [ '?-'] ),
      flush_output( O ).
  
  close_connection( N, I, O ) :-
      close( I ), 
      close( O ),
      socket_close( N ).
  
  fail_message( X, N ) :-
      format( "fail : ~w ~n", [X] ),
      socket_close( N ),
      fail.
      
  quit_connection( 'exit' ) :- !.    
  quit_connection( 'halt' ) :- !.    
  quit_connection( 'quit' ) :- !.    

more,
l8r,

------------------------------------------------------------------- 
Victor Bancroft, Principal Engineer, Zvolve Systems [v]770.551.4505 
1050 Crown Pointe Pkwy, Suite 300, Atlanta GA 30338 [f]770.551.4509 
Fellow, Artificial Intelligence Center              [v]706.542-0358 
Athens, Georgia  30602, U.S.A           http://ai.uga.edu/~bancroft 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]