Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % :- dynamic .
- create_server(Port) :-
- tcp_socket(Socket),
- tcp_bind(Socket, Port),
- tcp_listen(Socket, 5),
- acceptor_loop(Socket).
- on_thread_exit(Ip) :-
- writeln(user_error, "Exiting On Socket Closed!"),
- format(user_error,"Disconnection from: ~w\n",[Ip]),
- flush_output(user_error).
- % the client handling threaad doesn't die on error from the socket
- % need to make this happen I think
- acceptor_loop(Socket) :-
- tcp_accept(Socket,Client,Ip),
- thread_create(handle_client(Client,Ip),ThreadId,[at_exit(on_thread_exit(Ip))]),
- format("Connection from: ~w\n",[Ip]),
- flush_output,
- acceptor_loop(Socket).
- output(Stream,String) :-
- string_concat(String,"\r",Line),
- writeln(Stream,Line),
- flush_output(Stream).
- handle_client(Client,Ip) :-
- % thread_at_exit((writeln("DO WE EXIT?!"),flush_output)),
- tcp_open_socket(Client,Stream),
- output(Stream,"Welcome to Prolog Communications!"),
- client_loop(Stream).
- client_loop(Stream) :-
- read_string(Stream, "\n", "\r", End, Line),
- ( End == -1 ->
- writeln("Exiting thread...."),
- thread_exit("Socket Closed") ;
- output(Stream,Line),
- % close(Stream),
- writeln("everything is fine, looping for more input..."),
- client_loop(Stream)).
- start :- create_server(8000).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement