[erlang-questions] "Bad value on output port 'tcp_inet'" for null terminated messages

Per Hedeland per@REDACTED
Fri Sep 11 00:27:10 CEST 2009


Jani Launonen <launoja@REDACTED> wrote:
[Juhani Ränkimies <juhani@REDACTED> wrote:]
>> 
>> ok = gen_tcp:send(Socket, "message text"),
>> 
>> seems to work (the receiving end is of course confused), but

Why "of course"? Sending NUL-terminated strings over TCP is rather
uncommon.

>> ok = gen_tcp:send(Socket, "message text\0"),
>> 
>> gives
>> 
>> =ERROR REPORT==== 10-Sep-2009::20:21:21 ===
>> Bad value on output port 'tcp_inet'
>> 
>> 
>> How can I send the trailing 0-byte?

It should work as above, and does when I try it:

Eshell V5.7.2  (abort with ^G)
1> {ok,S0}=gen_tcp:listen(5678,[]).
{ok,#Port<0.431>}
2> {ok,S1}=gen_tcp:connect("localhost",5678,[]).
{ok,#Port<0.438>}
3> {ok,S2}=gen_tcp:accept(S0).
{ok,#Port<0.439>}
4> gen_tcp:send(S1,"message text\0").
ok
5> flush().
Shell got {tcp,#Port<0.439>,[109,101,115,115,97,103,101,32,116,101,120,116,0]}
ok

Are you using some "strange" options? I can't think of any that would
cause that result though. Or, as Chandru asked, are you really
describing the problem correctly?

>How about:
>ok = gen_tcp:send(Socket, ["message text", 0]).

That should work too.:-)

>"An I/O list is a binary or a (possibly deep) list of binaries or integers in the range 0..255."
>Says Erlang Reference Manual about Port BIFs.

Yes - it doesn't say that it is *required* to be deep, which is the
only difference between the "integer lists" "message text\0" and
["message text", 0].

6> gen_tcp:send(S1,["message text",0]).
ok
7> flush().                            
Shell got {tcp,#Port<0.439>,[109,101,115,115,97,103,101,32,116,101,120,116,0]}
ok

>> This is erl5.7.2 on XP.

I wouldn't think such breakage could be caused by Windows, but who
knows - maybe it has "binary" and "text" sockets... Above test is on
FreeBSD.

--Per Hedeland.


More information about the erlang-questions mailing list