In the end what I did was create a 1024 byte slice, then read the message from the connection, then shorten the slice to the number of integers read.
I'm programming a server that accepts an incoming connection from a client and then reads from it (via net.Conn.Read()). I'm going to be reading the message into a byte slice obviously and then processing it in an unrelated way, but the question is - how do I find out the length of this message first to create a slice of according length?
- 点赞 评论 复制链接分享
The solution selected as correct is not good. What happens if the messages is more than 1024 Bytes? You need to have a protocol of the form TLV (Type Length Value) or just LV if you don't have different types of messages. For example, Type can be 2 Bytes and Length can be 2 Bytes. Then you first always read 4 Bytes, then based on the length indicated in Bytes 2 and 3, you will know how many Bytes come later and then you read the rest. There is something else you need to take into account: TCP is stream oriented so in order to read the complete TCP message you might need to read many times. Read this (it is for Java but useful for any language): How to read all of Inputstream in Server Socket JAVA点赞 评论 复制链接分享
It is entirely dependent on the design of the protocol you are attempting to read from the connection.
If you are designing your own protocol you will need to design some way for your reader to determine when to stop reading or predeclare the length of the message.
For binary protocols, you will often find some sort of fixed size header that will contain a
lengthvalue (for example, a big-endian int64) at some known/discoverable header offset. You can then parse the value at the
lengthoffset and use that value to read the correct amount of data once you reach the offset beginning the variable length data. Some examples of binary protocols include
For text protocols, when to stop reading will be encoded in the parsing rules. Some examples of text protocols include
HTTP/1.1request for example, declares the protocol to look something like:
METHOD /path HTTP/1.1 Header-1: value Header-2: value Content-Length: 20 This is the content.
The first line (where line is denoted as ending with
Subsequent lines are defined to be headers, made up of a key and a value.
The key includes any text from the beginning of the line up to, but not including, the colon. After the colon comes a variable number of insignificant space characters followed by the value.
One of these headers is special and denotes the length of the upcoming body:
Content-Length. The value for this header contains the number of bytes to read as the body. For our simple case (ignoring trailers, chunked encoding, etc.), we will assume that the end of the body denotes the end of the request and another request may immediately follow.
After the last header comes a blank line denoting the end of the header block and the beginning of the body (
Once you are finished reading all the headers you would then take the value from the
Content-Lengthheader you parsed and read the next number of bytes corresponding to its value.
For more information check out:点赞 评论 复制链接分享
- java 访问ftp读文件
- nio socket 异步客户端读取时的问题！
- 3年swing,1年JavaEE,1年java socket转android什么待遇
- 有关读取UDP 数据包 不定长度的问题。
- 求助:netty 4.x服务器端出现CLOSE_WAIT的问题
- 各位大神帮个忙，socket接收返回数据报“Connection ”
- java写了一个聊天室的程序 但是私聊第二个人就报空指针异常 不知道为什么 求解