<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta content="text/html;charset=UTF-8" http-equiv="Content-Type"></head><body ><div style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt;"><div>I have a simple gen_server which connects to a tcp port, when I run gen_server directly:<br></div><div>erl -noshell -s ttest start<br></div><div>it works fine, But when I run:<br></div><div>erl -noshell -s tt_sup start_link <br></div><div>the connection will be closed instantly, it will work if I unlink the supervisor.<br></div><div>How could I run my simple gen_server with supervisor ?<br></div><div><br></div><div><br></div><div>-module(tt_sup).<br></div><div>-behaviour(supervisor).<br></div><div>-export([init/1, start_link/0, start_shell/0]).<br></div><div><br></div><div>start_link() -><br></div><div> io:format("Supervisor started with PID~p~n", [self()]),<br></div><div> {ok, Pid} = supervisor:start_link(tt_sup, []),<br></div><div> io:format("Supervisor PID=~p~n", [Pid]),<br></div><div> {ok, Pid}.<br></div><div><br></div><div>start_shell() -><br></div><div> io:format("Supervisor started with PID~p~n", [self()]),<br></div><div> {ok, Pid} = supervisor:start_link(tt_sup, []),<br></div><div> io:format("Supervisor PID=~p~n", [Pid]),<br></div><div> unlink(Pid),<br></div><div> {ok, Pid}.<br></div><div><br></div><div>init(_Args) -><br></div><div> SupFlags = #{strategy => one_for_one},<br></div><div> Child = [#{id => ttest, start => {ttest, start, []},<br></div><div> restart => permanent, shutdown => brutal_kill,<br></div><div> type => worker, modules => [ttest]}],<br></div><div> {ok, {SupFlags, Child}}.<br></div><div><br></div><div><br></div><div>-module(ttest).<br></div><div>-export([start/0,init/1,handle_cast/2,handle_call/3,handle_info/2,terminate/2,connect/1]).<br></div><div>-behaviour(gen_server).<br></div><div><br></div><div>start() -><br></div><div> io:format("gen_server start id is ~p~n",[self()]),<br></div><div> {ok,Pid} = gen_server:start_link({local,ttest},ttest,[],[]),<br></div><div> io:format("gen_server id is ~p~n",[Pid]),<br></div><div> connect(Pid),<br></div><div> {ok,Pid}.<br></div><div><br></div><div>init(_Args) -><br></div><div> io:format("init id is ~p~n",[self()]),<br></div><div> {ok,[]}. <br></div><div>handle_call({tcp,Socket,Bin},_From,States) -><br></div><div> {reply,States,States}.<br></div><div>handle_info({tcp,Socket,Bin},States) -><br></div><div> io:format("got info msg"),<br></div><div> {noreply,States}.<br></div><div><br></div><div>handle_cast({send,Msg},States) -><br></div><div> {reply,States}.<br></div><div><br></div><div>connect(I) -><br></div><div> io:format("connect id is ~p~n",[self()]),<br></div><div> {ok,Socket} =gen_tcp:connect("localhost",6000,[binary,{active,true}]),<br></div><div> gen_tcp:controlling_process(Socket,I).<br></div><div><br></div><div>terminate(shutdown, State) -><br></div><div> io:format("terminating"),<br></div><div> ok.<br></div></div><br></body></html>