I user gen_tcp:recv(Socket, 0).
for data receiveng, but i can receive only 1418 bytes for 1 time. How can I receive how much data was sent?
相关问题
- Multiple sockets for clients to connect to
- Drakma and Dexador both fails at USocket call whil
- difference between file descriptor and socket file
- Can we create a Silverlight Socket Server ONLY usi
- Is “new” in Erlang part of the official standard a
相关文章
- socket() returns 0 in C client server application
- Passing extra metadata to a RequestHandler using p
- How do I get the external IP of a socket in Python
- Native hooking in Android Client
- Is zeroing out the “sockaddr_in” structure necessa
- How do I modify a record in erlang?
- Check active timers in Erlang
- Does the TCPServer + BaseRequestHandler in Python&
in
gen_tcp:recv(Socket, 0)
you are asking the kernel: "Give me all data there is available right now in the receive buffer". The kernel is also free to give you less however. Even for a rather fast link, you will probably hit slow start on the TCP connection so in the beginning you will not get much data.The solution is to do your own buffering. You will have to eat data from the underlying socket until you have enough to construct a message. It is quite common for binary protocols to implement their own kind of messaging on top of the stream due to this.
For the longer term record: A common message format is to encode a message as:
That is, messages are 4 bytes representing a 32-bit bigendian integer followed by the payload, where the length is given by the integer. This format, and others like it, are so common Erlang includes optimized parsers for it directly in the C-layer. To get access to these, you set options on the socket through
inet/setops/2
, in our case we set{packet, 4}
. Then we can get messages by setting{active, once}
on the socket and wait for the next message. When it arrives, we can{active, once}
again on the socket to get the next message, and so on. There is an example in the documentation ofgen_tcp
(erl -man gen_tcp
if you have the Erlang man-pages installed appropriately).Other common formats are asn.1 or even http headers(!).
Tricks
It is often beneficial to create a process which is separate that can encode and decode your message format and then send on data to the rest of the system. Usually a good solution in Erlang is to demux incoming data as fast as possible and get the data to a process which can then handle the rest of the problem.