你可以使用mime/multipart reader来读取从Conn中获取的数据。

huangapple go评论93阅读模式
英文:

Can I use mime/multipart reader for data read from a Conn?

问题

我通过TCP套接字发送了一个带有边界分隔的JSON数据。我了解到golang有一个mime/multipart读取器,我试图利用它,但没有成功。

相关的代码如下:

//我创建了一个围绕在Conn类型上的读取器,其中包含之前定义的边界
reader := multipart.NewReader(conn, MESSAGE_BOUNDARY)

//然后我有一个gopher从中读取部分/数据:
part, _ := reader.NextPart()
var line []byte
part.Read(line)

问题是,NextPart()会阻塞,而且永远不会通过。我从未收到一个PART,尽管我正在接收数据(尝试在尝试获取下一个部分之前从Conn中读取等)。我尝试用bufio读取器替换multipart读取器,并且使用ReadString可以得到我想要的结果。问题是,我不能使用单个字节作为分隔符,因为我传递的是可能包含各种字节的大型消息。任何帮助将不胜感激。

英文:

I am sending a BOUNDARY delimited JSON data through a TCP socket. I read that golang has a mime/multipart readers and I tried to utilize it, to no avail.

The code in question is:

//I create a reader wrapped around a Conn type with boundary previously defined
reader := multipart.NewReader(conn, MESSAGE_BOUNDARY)

//Then I have a gopher which reads the parts/data from it:
part, _ := reader.NextPart()
var line []byte
part.Read(line)

The issue is, NextPart() blocks and it's never passed. I never get a PART, even though I am receiving data (tried reading from the Conn before trying to get the next part and so on). I tried changing the multipart reader with a bufio reader, and with ReadString I got a what I was suppose to. Problem is, I can't use a single byte to delimit, since I'm passing large messages that might contain all kinds of bytes. Any help would be appreciated.

答案1

得分: 1

请验证您的数据是否与mime/multipart Reader期望的格式匹配。在您的情况下,reader.NextPart()可能会被阻塞,等待正确的边界或每个部分的头部。

参考NewReader中的示例:

--MESSAGE_BOUNDARY\r\n
Header1: Value\r\n
Header2: Value\r\n
\r\n
<part data>\r\n
--MESSAGE_BOUNDARY\r\n
Header1: Value\r\n
\r\n
<part data>\r\n
--MESSAGE_BOUNDARY--\r\n

另外,请确保检查从NextPart()返回的错误值,它会告诉您是否存在MIME编码问题或通用I/O错误。

最后,上述代码中的part.Read(line)将无法正常工作,因为您的line缓冲区容量为0字节。如果可以将每个部分缓冲到内存中,那么可以像NewReader示例中那样使用io/ioutil ReadAll。否则,请将line声明为line := make([]byte, 4096),并使用循环以4KB的块读取部分数据。

英文:

Verify that your data matches the format expected by the mime/multipart Reader. In your case reader.NextPart() may be blocked waiting for a proper boundary or per-part headers.

See the example under NewReader for reference:

--MESSAGE_BOUNDARY\r\n
Header1: Value\r\n
Header2: Value\r\n
\r\n
<part data>\r\n
--MESSAGE_BOUNDARY\r\n
Header1: Value\r\n
\r\n
<part data>\r\n
--MESSAGE_BOUNDARY--\r\n

Also, make sure to check the error value returned from NextPart(), it will tell you if there is a MIME encoding problem or a generic I/O error.

Lastly, the part.Read(line) will not work as written above, since your line buffer has a 0-byte capacity. If it is OK to buffer each part in memory, then use io/ioutil ReadAll as in the NewReader example. Otherwise, declare line as line := make([]byte, 4096) and use a loop to read out the part data in 4KB chunks.

huangapple
  • 本文由 发表于 2014年7月7日 19:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/24609145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定