2011-08-19 3 views
0

숙제 연습을 위해 스트리밍 비디오 서버를 구축하고 있습니다. 이제 청크 분할 전송 인코딩을 사용하여 클라이언트에 데이터를 보내려고합니다. 내 코드 :청크 분할 전송 인코딩을 사용하여 클라이언트에 데이터 전송

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); 

그러나 클라이언트는 실제 데이터를 수신하지 않습니다.

TcpListener listener = new TcpListener(ipe);//ipe has my computer ip address and port's 80 
Socket socket = listener.AcceptSocket(); 
NetworkStream ns = new NetworkStream(socket); 

보내 나에게 올바른 길을 제시해주십시오 : 나는 패킷을 캡처 와이어 샤크를 사용하고 난 클라이언트가 청크 분할 인코딩이 최종 수신 참조 : 나는 클라이언트에서 연결을 듣고 TcpListener를 사용하고

HTTP chunked response 

End of chunked encoding 
      Chunk size: 0 octets 
      Chunk boundary 
End of chunked encoding 
      Chunk size: 0 octets 
      Chunk boundary 

을 데이터를 청크 분할 전송 인코딩을 사용하여 클라이언트에 전송합니다. 감사합니다.

+1

당신은 http impelementation에 대해 아무 것도 쓰지 않습니다 ... TcpListener 당 자체입니까? HttpListener에 내장되어 있습니까? 아니면 IIS HttpHandler/HttpModule을 통해 무엇입니까? – Yahia

+0

@Yahia : TcpListener를 사용하여 클라이언트의 연결을 청취합니다. – PenguinSh

+0

왜 TcpListener를 사용하고 있습니까? HttpListener는 IIS와 비슷한 커널 모듈 인 HTTP.SYS를 사용하며 매우 성능이 뛰어납니다. 또한 Chunked와 다른 많은 것들을 포함하여 HTTP에 대한 구현이 있습니다. – Yahia

답변

0

청크 블록 길이를 인코딩하는 방법이 잘못되었다고 생각합니다. 청크 분할 전송 인코딩에서 각 블록의 길이는 다음과 같이 ASCII 문자열로 인코딩되어야합니다.

byte[] chunkedSize = System.Text.Encoding.ASCII.GetBytes("4\r\n\r\n"); 
    byte[] end = System.Text.Encoding.ASCII.GetBytes("0\r\n\r\n"); 
관련 문제