'recbuf' and the size of received data

Hakan Mattsson <>
Mon Jun 25 17:44:47 CEST 2001


On Sun, 24 Jun 2001, Scott Lystig Fritchie wrote:

Scott> >>>>> "wb" == Willem Broekema <> writes:
Scott> 
Scott> wb> I have a problem receiving tcp packets with data larger than 1024
Scott> wb> bytes.
Scott> 
Scott> If you attempt to read N bytes from a TCP socket, you'll get at most N
Scott> bytes, but all other bets are off.  If the sender is transmitting in M
Scott> byte chunks, you may get M bytes when attempting to read N bytes
Scott> (assuming M < N), but you may not.  If you need message boundaries,
Scott> you need to use a different protocol (e.g. UDP) or you need to embed
Scott> those boundaries into the protocol you're using over TCP.

If you want to use TCP you may utilize the builtin Erlang emulator
support for TPKT (http://www.ietf.org/rfc/rfc1006.txt).

Open the socket with:

	gen_tcp:connect(Host, Port, [{packet, tpkt} | OtherOptions])

Add the TPKT header manually on the sending side:

	tpkt_send(Socket, MessageBody) when binary(MessageBody) ->
	    Len = size(MessageBody) + 4,
	    gen_tcp:send(Socket, [3, 0, ((Len) bsr 8) band 16#ff, (L) band 16#ff , MessageBody]).

On the receiving side, the Erlang emulator will automatically assemble
complete TPKT-packets before they are forwarded to the control
process, where you strip the header off with:

	handle_info({tcp, Socket, <<3:8, _:8, Len:16, MessageBody/binary>>}, State) ->
            decode_body(MessageBody, State)...

/Håkan

---
Håkan Mattsson
Ericsson
Computer Science Laboratory
http://www.ericsson.se/cslab/~hakan




More information about the erlang-questions mailing list