Capstone: TCP server
| Start the TCP server | |
| Connect and send | |
| Broadcast to all | |
| Close the connection |
Start the TCP server
The server loops on a socket, accepting connections and spawning a handler for each client. The select function watches the socket for incoming data and new connections.
-module(tcp_server).
-behaviour(gen_server).
-export([start/1]).
-start_link(Port) ->
{ok, ListenSocket} = gen_tcp:listen(Port, [binary, packet, 0]).
-handle_info({tcp, Socket, Data}, State) ->
io:format("Client said: ~s~n", [Data]),
gen_server:reply(Sender, ok).
loop(Socket, State).
gen_tcp:recv(Socket, 0),
after loop(Socket, State).
Next: Performance
Article author: Arthur Isaev