TCP sockets

View: New views
3 Messages — Rating Filter:   Alert me  

TCP sockets

by Khalil Hafsi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi guys,

I am a little bit confused about the description of the library(socket) , I want to create a little service ,  i.e server however I found these examples sadly do not work ...
the Examples are taken from the docum. :

library(socket).
create_server(Port) :-tcp_socket(Socket),
    tcp_bind(Socket, Port),
    tcp_listen(Socket, 5),
    write(Port),
    tcp_open_socket(Socket,AcceptFd, _),
    dispatch(AcceptFd).

dispatch(AcceptFd) :-
        tcp_accept(AcceptFd, Socket, _Peer),
        fork(Pid)
        (   Pid == child
        ->  tcp_open_socket(Socket, In, Out),
            handle_service(In, Out),
            close(In),
            close(Out),
            halt
        ;   tcp_close_socket(Socket)
        ),dispatch(AcceptFd).

Any way so this little program does not get compiled , it complains about a missing operator ..
I also wanted to know where can I find the meaning  of "->" and the "( and )" in the above example , I can't search them because they get always ignored.
Ok now Say I want my main application running and I want to pass the IO that I normally send/receive through Screen/Keybo. over TCP.
Is that possible ? Is there any working examples that I could read ?

Thanks for the help !
Khalil

_______________________________________________ SWI-Prolog mailing list SWI-Prolog@... https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: TCP sockets

by Nicolas Pelletier-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

On Wed, Oct 7, 2009 at 4:49 AM, Khalil Hafsi <hafsi@...> wrote:
>
> I am a little bit confused about the description of the library(socket) , I
> want to create a little service ,  i.e server however I found these examples
> sadly do not work ...

The documentation is a bit wrong here; I think you should have
overcome this by trying to do simpler things first, though. Here is a
minimal, working session. The output is not pretty-printed, but the
info is here (port number and stream on the server side, the atom
'Roger' on the client side).

HTH,

--
Nicolas

<<On terminal 1>>
nicolas@dynamo:~$ pl
% library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,616 bytes
% /home/nicolas/.plrc compiled 0.01 sec, 2,152 bytes
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.7.15)
Copyright (c) 1990-2009 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- [user].
|: :- use_module(library(socket)).
%     library(error) compiled into error 0.00 sec, 18,512 bytes
%    library(lists) compiled into lists 0.00 sec, 42,464 bytes
%   library(shlib) compiled into shlib 0.01 sec, 63,448 bytes
%  library(socket) compiled into socket 0.01 sec, 76,048 bytes
|: create_server(Port) :-
|:     tcp_socket(Socket),
|:     tcp_bind(Socket, Port),
|:     tcp_listen(Socket, 5),
|:     write(Port),
|:     tcp_open_socket(Socket,AcceptFd, _),
|:     dispatch(AcceptFd).
|:
|: dispatch(AcceptFd) :-
|:     tcp_accept(AcceptFd, Socket, _Peer),
|:     fork(Pid),
|:     (   Pid == child
|:     ->  tcp_open_socket(Socket, In, Out),
|:         handle_service(In, Out),
|:         close(In),
|:         close(Out),
|:         halt
|:     ;   tcp_close_socket(Socket)
|:     ),
|:     dispatch(AcceptFd).
|:
|: handle_service(In, Out) :-
|:     write(In),
|:     write(Out, 'Roger').
|: % user://1 compiled 0.04 sec, 79,248 bytes
true.

?- create_server(60000).
60000$stream(304520)

<<On terminal 2>>
nicolas@dynamo:~$ telnet localhost 60000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
RogerConnection closed by foreign host.
nicolas@dynamo:~$
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: TCP sockets

by jeffrose :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all:

The example shown is minimally complete. It may work for demonstration purposes but fails to
satifsy a critically important safety requirement: it completely ignores the prospect of trouble -- a likely
occurance in socket communication.

If anything bad happens to either the server *or* client in the course of a session, one of two things (and perhaps both) will likely happen:
1) any exception will doom both server and client, and 2) the server and client will leak their respective sockets, but *worse*, the server will
leak the port number. The server cannot be restarted because the leaked socket is still bound to the port
and the port cannot be reused *on that machine* until the socket that's using it is closed.

We must all be more careful.
And these days, we should never use forks, when threads will do. They're safer
and easier to keep track of.

The attached example is not completely free of hazard. For example, the server will leak Socket1 (line 25), if the handler thread fails to start,
but it is safe for most things that will likely happen.

I wonder also how setup_call_cleanup/3 is supposed to behave in tail-recursive loops. My guess is badly...uh...unfavorably.

Regards,
Jeff R.




-----Original Message-----
From: Nicolas Pelletier <sxatrxtfxcrwamvjkaga@...>
To: SWI Prolog Mailing list <swi-prolog@...>
Sent: Tue, Oct 6, 2009 9:48 pm
Subject: Re: [SWIPL] TCP sockets

Hello,

On Wed, Oct 7, 2009 at 4:49 AM, Khalil Hafsi <hafsi@...> wrote:
>
> I am a little bit confused about the description of the library(socket) , I
> want to create a little service ,  i.e server however I found these examples
> sadly do not work ...

The documentation is a bit wrong here; I think you should have
overcome this by trying to do simpler things first, though. Here is a
minimal, working session. The output is not pretty-printed, but the
info is here (port number and stream on the server side, the atom
'Roger' on the client side).

HTH,

--
Nicolas

<<On terminal 1>>
nicolas@dynamo:~$ pl
% library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,616 bytes
% /home/nicolas/.plrc compiled 0.01 sec, 2,152 bytes
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.7.15)
Copyright (c) 1990-2009 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- [user].
|: :- use_module(library(socket)).
% library(error) compiled into error 0.00 sec, 18,512 bytes
% library(lists) compiled into lists 0.00 sec, 42,464 bytes
% library(shlib) compiled into shlib 0.01 sec, 63,448 bytes
% library(socket) compiled into socket 0.01 sec, 76,048 bytes
|: create_server(Port) :-
|: tcp_socket(Socket),
|: tcp_bind(Socket, Port),
|: tcp_listen(Socket, 5),
|: write(Port),
|: tcp_open_socket(Socket,AcceptFd, _),
|: dispatch(AcceptFd).
|:
|: dispatch(AcceptFd) :-
|: tcp_accept(AcceptFd, Socket, _Peer),
|: fork(Pid),
|: ( Pid == child
|: -> tcp_open_socket(Socket, In, Out),
|: handle_service(In, Out),
|: close(In),
|: close(Out),
|: halt
|: ; tcp_close_socket(Socket)
|: ),
|: dispatch(AcceptFd).
|:
|: handle_service(In, Out) :-
|: write(In),
|: write(Out, 'Roger').
|: % user://1 compiled 0.04 sec, 79,248 bytes
true.

?- create_server(60000).
60000$stream(304520)

<<On terminal 2>>
nicolas@dynamo:~$ telnet localhost 60000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
RogerConnection closed by foreign host.
nicolas@dynamo:~$
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog


tmp32.pl (1K) Download Attachment