<meta http-equiv="Content-Type" content="text/html; charset=GB18030"><div>Hi,</div><div><br></div><div>I'm sorry, my previous question is not clear.</div><div><br></div><div>I start a external process by erlang:open_port, and use two bytes for message length. But erlang process will not receive the message when the message's length of external process return is 10 bytes, and it will lead to all the follow messages can't be received. Using OTP 19.</div><div><br></div><div><span style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";">This is how I'm using it:</span></div><div><br></div><div>%% erlang</div><div><div style=""><font face="lucida Grande, Verdana, Microsoft YaHei">init([Src]) -></font></div><div style=""><font face="lucida Grande, Verdana, Microsoft YaHei">    erlang:process_flag(trap_exit, true),</font></div><div style=""><font face="lucida Grande, Verdana, Microsoft YaHei">    ServerId = erlang:integer_to_list(args_system:get_sid(Src)),</font></div><div style=""><font face="lucida Grande, Verdana, Microsoft YaHei">    Port = case os:type() of</font></div><div style=""><font face="lucida Grande, Verdana, Microsoft YaHei">               {win32, _} -></font></div><div style=""><font face="lucida Grande, Verdana, Microsoft YaHei">                   open_port({spawn_executable, "./erlc_win32"}, [{packet, 2}, {args, [ServerId]}]);</font></div><div style=""><font face="lucida Grande, Verdana, Microsoft YaHei">               _ -></font></div><div style=""><font face="lucida Grande, Verdana, Microsoft YaHei">                   open_port({spawn_executable, "./erlc_unix"}, [{packet, 2}, {args, [ServerId]}])</font></div><div style=""><font face="lucida Grande, Verdana, Microsoft YaHei">           end,</font></div><div style=""><font face="lucida Grande, Verdana, Microsoft YaHei">    {ok, #state{port = Port}}.</font></div></div><div style=""><font face="lucida Grande, Verdana, Microsoft YaHei"><br></font></div><div style=""><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";">handle_call({call, Cmd, Msg}, From, #state{port = Port} = State) -></div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";">    case call_encode(Cmd, From, Msg) of</div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";">        false -></div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";">            {reply, false, State};</div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";">        Data -></div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";">            Port ! {self(), {command, Data}},</div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";">            {noreply, State}</div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";">    end;</div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";">handle_call(_Request, _From, State) -></div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";">    {reply, ok, State}.</div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";"><br></div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";"><div><div>handle_info({Port, {data, Data}}, #state{port = Port} = State) -></div><div>    io:format("Len = ~p~n", [length(Data)]),</div><div>    case decode(Data) of</div><div>        {From, Msg} -></div><div>            z_lib:reply(From, Msg);</div><div>        _ -></div><div>            ok</div><div>    end,</div><div>    {noreply, State};</div><div>handle_info({'EXIT', Port, Reason}, #state{port = Port} = State) -></div><div>    exit(Reason),</div><div>    {noreply, State};</div><div>handle_info(_Info, State) -></div><div>    {noreply, State}.</div></div><div></div></div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";"><br></div><div style="font-family: "lucida Grande", Verdana, "Microsoft YaHei";"><span style="font-family: Verdana;">// c</span></div><div style="">int main(int argc, char* argv[]) {</div></div><div><div><div><span style="white-space:pre">   </span>int i;</div><div><span style="white-space:pre">        </span>char out[1000] = {0};</div><div><span style="white-space:pre"> </span>for(i = 0; i < 1000; ++i)</div><div><span style="white-space:pre">  </span>{</div><div><span style="white-space:pre">             </span>strcat(out, "|");</div><div><span style="white-space:pre">           </span>write_cmd(out, strlen(out));</div><div><span style="white-space:pre">  </span>}</div></div><div>}</div><div><br></div><div><div>int write_cmd(unsigned char* buf, int len)</div><div>{</div><div><span style="white-space:pre">    </span>char li;</div><div><span style="white-space:pre">      </span>li = (len >> 8) & 0xff;</div><div><span style="white-space:pre">     </span>write_exact(&li, 1);</div><div><span style="white-space:pre">      </span>li = len & 0xff;</div><div><span style="white-space:pre">  </span>write_exact(&li, 1);</div><div><span style="white-space:pre">      </span>return write_exact(buf, len);</div><div>}</div></div><div><br></div><div><div>int write_exact(unsigned char* buf, int len)</div><div>{</div><div><span style="white-space:pre">      </span>int i, wrote = 0;</div><div><span style="white-space:pre">     </span>while (wrote < len)</div><div><span style="white-space:pre">        </span>{</div><div><span style="white-space:pre">             </span>if ((i = write(1, buf + wrote, len - wrote)) <= 0) </div><div><span style="white-space:pre">                   </span>return (i);</div><div><span style="white-space:pre">           </span>wrote += i;</div><div><span style="white-space:pre">   </span>}</div><div><span style="white-space:pre">     </span>return(len);</div><div>}</div></div><div><br></div><div>See attachment for test result.</div><div><br></div><div>If I modify the write_cmd function as follows, it will runs correct.</div><div><br></div><div><div><div>int write_cmd(unsigned char* buf, int len)</div><div>{</div><div><span style="white-space:pre">      </span>if ((len & 0xff) == 10)</div><div><span style="white-space:pre">   </span>{</div><div><span style="white-space:pre">             </span>len += 1;</div><div><span style="white-space:pre">     </span>}</div><div><span style="white-space:pre">     </span>char li;</div><div><span style="white-space:pre">      </span>li = (len >> 8) & 0xff;</div><div><span style="white-space:pre">     </span>write_exact(&li, 1);</div><div><span style="white-space:pre">      </span>li = len & 0xff;</div><div><span style="white-space:pre">  </span>write_exact(&li, 1);</div><div><span style="white-space:pre">      </span>return write_exact(buf, len);</div><div>}</div></div><div></div></div><div><br></div><div><br></div><div></div></div>