Advertisement
Guest User

Untitled

a guest
Feb 4th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.86 KB | None | 0 0
  1. load_netlayer:-
  2.     create_netlayer_server(Socket, Port, Stream),
  3.     thread_create(handle_incoming_connections(Stream), NetLayerThread, [detached(true)]).
  4.  
  5. handle_incoming_connections(Stream):-
  6.     write('waiting to connect'),
  7.     tcp_accept(Stream, ClientSocket, ClientIP),
  8.     write('accepted connection!'),
  9.     thread_create(create_client(ClientSocket, ClientIP), ClientThread, [detatched(true)]),
  10.     handle_incoming_connections(Stream).
  11.  
  12. create_client(Socket, IP):-
  13.     setup_call_cleanup(tcp_open_socket(Socket, Stream), handle_client(Socket, IP, Stream), close_connection(Stream)).
  14.    
  15. create_netlayer_server(Socket, Port, Stream):-
  16.     Port = 42069,
  17.     tcp_socket(Socket),
  18.     tcp_bind(Socket, Port),
  19.     tcp_listen(Socket, 5),
  20.     tcp_open_socket(Socket, Stream).
  21.  
  22. handle_client(Socket, IP, Stream):-
  23.     writef('hello %w %w', [Socket, IP]).
  24.  
  25. close_connection(Stream):-
  26.     close(Stream).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement