View difference between Paste ID: KgQjRNTC and hZY7YHgj
SHOW: | | - or go back to the newest paste.
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)).
13+
	setup_call_cleanup(tcp_open_socket(Socket, Stream),
14
               handle_client(Socket, IP, Stream), close_connection(Stream)).
15
	
16
create_netlayer_server(Socket, Port, Stream):-
17
	Port = 42069,
18
	tcp_socket(Socket),
19
	tcp_bind(Socket, Port),
20
	tcp_listen(Socket, 5),
21
	tcp_open_socket(Socket, Stream).
22-
handle_client(Socket, IP, Stream):-
22+
23-
	writef('hello %w %w', [Socket, IP]).
23+
handle_client(Socket, IP, In, Out):-
24
     tcp_open_socket(Socket, In, Out),
25
	set_stream(In, close_on_abort(false)),
26
	set_stream(Out, close_on_abort(false)),
27
	writef(Out ,'hello %w %w', [Socket, IP]).
28
29
close_connection(Stream):-
30
	close(Stream).