使用分块传输编码将数据发送到客户端
我正在为家庭作业构建一个流视频服务器,现在我想使用分块传输编码将数据发送到客户端。这是我的代码:
string statusLine = "HTTP/1.1 200 OK\r\n";
string server = "Server: Cougar/12.0.7600.16385\r\n";
string cacheControl = "Cache-Control: no-cache\r\n";
string desRespPragma1 = "Pragma: no-cache\r\n";
string desRespPragma3 = "Pragma: client-id=" + clientId + "\r\n";
string desRespPragma4 = "Pragma: features=" + "\"" + "seekable,stridable" + "\"" + "\r\n";
string transferEncoding = "Transfer-Encoding: chunked\r\n\r\n";
string desResp = statusLine + server + cacheControl + desRespPragma1 + desRespPragma3+ desRespPragma4 + transferEncoding;
byte [] status = Encoding.ASCII.GetBytes(desResp);
SendData(status);//In SendData function, I’m using NetworkStream to send data
//Send chunked size with CRLN
string CRLF = "\r\n";
byte[] crlf = Encoding.ASCII.GetBytes(CRLF);
byte[] chunkedSize = new byte[3];
chunkedSize[0] = 0x4;
Array.Copy(crlf, 0, chunkedSize, 1, 2);
SendData(chunkedSize);
//Send data
SendData(tHeader);//tHeader’s byte array, length is 4
//Send \r\n to delimeter
SendData(crlf);
//Send chunked size is 0 with \r\n\r\n to end.
byte[] end = new byte[5];
end[0] = 0;
Array.Copy(crlf, 0, end, 1, 2);
Array.Copy(crlf, 0, end, 3, 2);
SendData(end);
但是客户端没有收到真实数据。我使用 Wireshark 捕获数据包,我看到客户端收到分块编码的 2 端:
HTTP chunked response
End of chunked encoding
Chunk size: 0 octets
Chunk boundary
End of chunked encoding
Chunk size: 0 octets
Chunk boundary
我正在使用 TcpListener 侦听来自客户端的连接:
TcpListener listener = new TcpListener(ipe);//ipe has my computer ip address and port's 80
Socket socket = listener.AcceptSocket();
NetworkStream ns = new NetworkStream(socket);
请向我展示使用分块传输编码将数据发送到客户端的正确方法。我很感激。
I’m building a Streaming Video Server for a homework exercise, now I want to send data to client using Chunked Transfer Encoding. This is my code :
string statusLine = "HTTP/1.1 200 OK\r\n";
string server = "Server: Cougar/12.0.7600.16385\r\n";
string cacheControl = "Cache-Control: no-cache\r\n";
string desRespPragma1 = "Pragma: no-cache\r\n";
string desRespPragma3 = "Pragma: client-id=" + clientId + "\r\n";
string desRespPragma4 = "Pragma: features=" + "\"" + "seekable,stridable" + "\"" + "\r\n";
string transferEncoding = "Transfer-Encoding: chunked\r\n\r\n";
string desResp = statusLine + server + cacheControl + desRespPragma1 + desRespPragma3+ desRespPragma4 + transferEncoding;
byte [] status = Encoding.ASCII.GetBytes(desResp);
SendData(status);//In SendData function, I’m using NetworkStream to send data
//Send chunked size with CRLN
string CRLF = "\r\n";
byte[] crlf = Encoding.ASCII.GetBytes(CRLF);
byte[] chunkedSize = new byte[3];
chunkedSize[0] = 0x4;
Array.Copy(crlf, 0, chunkedSize, 1, 2);
SendData(chunkedSize);
//Send data
SendData(tHeader);//tHeader’s byte array, length is 4
//Send \r\n to delimeter
SendData(crlf);
//Send chunked size is 0 with \r\n\r\n to end.
byte[] end = new byte[5];
end[0] = 0;
Array.Copy(crlf, 0, end, 1, 2);
Array.Copy(crlf, 0, end, 3, 2);
SendData(end);
But client don’t receive true data. I use Wireshark to capture packet and I see client receives 2 end of chunked encoding :
HTTP chunked response
End of chunked encoding
Chunk size: 0 octets
Chunk boundary
End of chunked encoding
Chunk size: 0 octets
Chunk boundary
I'm using TcpListener to listen connection from client :
TcpListener listener = new TcpListener(ipe);//ipe has my computer ip address and port's 80
Socket socket = listener.AcceptSocket();
NetworkStream ns = new NetworkStream(socket);
Please show me the correct way to send data to client using Chunked Transfer Encoding. I appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你对分块块长度进行编码的方式是错误的。在分块传输编码中,每个块的长度也应该编码为 ASCII 字符串,例如:
I think that your way to encode the chunked block length is wrong. In chunked transfer encoding, each block's length should also be encoded to ASCII strings, like: