C# TCP/IP 패킷 압축전송 DeflateStream질문 드립니다.
먼저 코드부터 보여드리겠습니다...
Server
byte[] sizebuf = new byte[sizeof(long)]; //받을 데이터 사이즈 크기 확인
screen_ns.Read(sizebuf, 0, sizeof(long));
screen_ns.Flush();
long size = BitConverter.ToInt64(sizebuf, 0);
if (size < 0 || size > 500000)
continue;
byte[] buf = new byte[size];
screen_ns.Read(buf, 0, buf.Length); //클라이언트로부터 값 리스브
MemoryStream ms = new MemoryStream(buf);
//ms.Write(buf, 0, buf.Length);
//ms.Position = 2;
MemoryStream ms2 = new MemoryStream();
using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress))
{
ds.CopyTo(ms2);
ds.Dispose();
te.Dispatcher.Invoke(new UpdataScreenDelegate(UpdataScreen), ms);
}
screen_ns.Flush();
Client
//화면얻어오기
System.Drawing.Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;
System.Drawing.Size size = new System.Drawing.Size(rect.Width, rect.Height);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(rect.Width, rect.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.CopyFromScreen(0, 0, 0, 0, size);
//얻어온 화면 메모리스트림에 쓰기
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Seek(0, SeekOrigin.Begin);
MemoryStream ms2 = new MemoryStream();
DeflateStream ds = new DeflateStream(ms2, CompressionMode.Compress);//압축하기
ms.CopyTo(ds);
byte[] buf = new byte[ms.Length];
ms2.Seek(0, SeekOrigin.Begin);
ms2.Read(buf, 0, Convert.ToInt32(ms2.Length)); //buf에 쓰기
screenns.Write(BitConverter.GetBytes(ms2.Length), 0, sizeof(long)); //보낼 사이즈 전송
screenns.Flush();
Delay(80);
screenns.Write(buf, 0, buf.Length); //서버로 보내기
screenns.Flush();
-------------------------------------------------------------------------------------------------------------------
클라이언트는 자신의 컴퓨터 화면을 캡처하여 서버로 보내는 소스입니다.
그런데 DeflateStream으로 압축하여 보내서 서버로 받는것 까진 잘 됩니다.
문제는 서버에서 받은 이후
ds.CopyTo(ms2);
에서 "블록길이가 보수와 일치하지 않습니다.(block length does not match with its complement)" 오류가 발생합니다.
우선 구글링 결과 자바와 .Net의 통신인 경우 이런 오류가 인코딩때문에 발생 하여
ms.Position = 2;
2byte만 이동하여 압축을 해제하면 해결 된다는 글을 보아 혹시나 하여 사용 했지만...
"알 수 없는 블록 형식입니다. 스트림이 손상되었을 수 있습니다."
라는 오류가 뜨더군요.
전송 패킷을 압축하여 보내려고 했는데 이런 문제에 부딪힐 줄은 몰랐습니다.
어제 하루 종일 이것만 붙들고 있다가 도저희 혼자서는 해결을 할 수 없어서
도움을 요청해 봅니다.
감사합니다.
댓글 달기