1
SslStream
을 사용하여 클라이언트와 서버 간의 TCP 연결을 암호화하고 있습니다. 문제는 클라이언트가 데이터를 읽을 때 실제 데이터 대신에 0 바이트의 묶음이 주어질 수 있다는 것입니다.이상한 SslStream 버퍼링 문제
// Server
using (NetworkStream tcpStream = client.GetStream())
{
Stream stream = tcpStream;
if (ssl)
{
SslStream sslStream = new SslStream(tcpStream, true);
sslStream.AuthenticateAsServer(cert, false, SslProtocols.Default, false);
stream = sslStream;
}
byte[] buf = new byte[] {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02};
stream.Write(buf, 0, buf.Length);
buf = new byte[] {0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
stream.Write(buf, 0, buf.Length);
}
// Client
using (NetworkStream tcpStream = client.GetStream())
{
Stream stream = tcpStream;
if (ssl)
{
SslStream sslStream = new SslStream(
tcpStream,
true,
delegate { return true; }
);
sslStream.AuthenticateAsClient(
"localhost",
null,
SslProtocols.Default,
false
);
stream = sslStream;
}
byte[] buf = new byte[7];
stream.Read(buf, 0, buf.Length);
// buf is 01010101010101 as expected
buf = new byte[9];
stream.Read(buf, 0, buf.Length);
// buf is 020000000000000000 instead of the expected 020303030303030303
// a subsequent read of 8 bytes will get me 0303030303030303
// if the ssl bool is set to false, then the expected data is received without the need for a third read
}
그것은 클라이언트가 서버가 SslStream
를 사용하는 경우에만 그들을 쓴 바이트 동일한 수의 스트림에서 읽을 필요가 것처럼 나타납니다 여기에 문제를 보여주는 예입니다. 이건 옳지 않아. 내가 여기서 무엇을 놓치고 있니?
아, 꽤 아마추어 실수를. 나는 TcpStream이 항상 내가 원했던 것을 정확하게 준 이후로 나는 단지 행복하게 무식하다고 생각한다. 그때 내가 스트림에서 읽는 코드를 가지고 있다면 여기서 설명한 논리를 사용해야한다고 생각합니다. 스트림 읽기가 끝날 때마다 유틸리티 기능을 제공하는 꽤 좋은 후보자처럼 보입니다. – Dennis