public void ReceiveImage() { int GetImageLength; int GetImageArea; int RealImageLength; MemoryStream ms = new MemoryStream(); IPBindPoint_ForImage = new IPEndPoint(ServerIP, sPort + 1); IPBindPoint_ForImage = new IPEndPoint(((IPEndPoint)Client[0].RemoteEndPoint).Address, sPort + 1); senderp = (IPEndPoint)IPBindPoint_ForImage; Bitmap GetImageBit; //Thread.Sleep(10000); try { ImageServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 500); while (Client[0].Connected) { //Get Image Size //ImageServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 0); Client[0].Receive(GetImageSize, 0, 4, SocketFlags.None); RealImageLength = BitConverter.ToInt32(GetImageSize, 0); Client[0].Receive(GetImageSize, 0, 4, SocketFlags.None); GetImageLength = BitConverter.ToInt32(GetImageSize, 0); if (GetImageLength == 0) { continue; } //Get Image Area /*ImageClient.Receive(GetImageAreaByte, 0, 4, SocketFlags.None); * GetImageArea = BitConverter.ToInt32(GetImageAreaByte, 0);*/ GetImageBytes = new byte[GetImageLength]; //ImageServer.Receive(GetImageBytes, 0, GetImageLength, SocketFlags.None); //ImageServer.ReceiveFrom(GetImageBytes, 0, GetImageLength, SocketFlags.None, ref senderp); int count = 0; int icount = 0; sw = false; do { try { GetByte = new byte[SizeForOne + 4]; for (int i = icount; i < GetImageLength / SizeForOne; i++) { //ChatBoxAdd(ImageServer.ReceiveFrom(GetByte, 0, SizeForOne + 4, SocketFlags.None, ref senderp).ToString()); ImageServer.BeginReceiveFrom(GetByte, 0, SizeForOne + 4, SocketFlags.None, ref senderp, new AsyncCallback(MessageCallBack), Tuple.Create(GetByte, 0, GetImageLength)); } Client[0].Send(new byte[4]); Client[0].Receive(new byte[4]); if (GetImageLength % SizeForOne != 0) { ImageServer.ReceiveFrom(GetImageBytes, GetImageLength - (GetImageLength % SizeForOne), GetImageLength % SizeForOne, SocketFlags.None, ref senderp); } sw = true; Client[0].Send(BitConverter.GetBytes(-1)); } catch (Exception Ex3) { ChatBoxAdd(Ex3.ToString()); ChatBoxAdd("UDP 데이터 소실 발생."); sw = false; icount = 0; Client[0].Send(BitConverter.GetBytes(-1)); break; Client[0].Send(BitConverter.GetBytes(icount)); } } while (!sw); try { if (sw == false) { throw new Exception(); } ms = new MemoryStream(); ms.Write(GetImageBytes, 0, (int)GetImageBytes.Length); ms.Position = 0; GetImageBytes = new byte[RealImageLength]; //var DecompressStream = new DeflateStream(ms, CompressionMode.Decompress,true); var DecompressStream = new GZipStream(ms, CompressionMode.Decompress, true); var DecompressedStream = new MemoryStream(); DecompressStream.CopyTo(DecompressedStream); DecompressStream.Close(); DecompressedStream.Position = 0; Bitmap Image = new Bitmap(DecompressedStream); //Bitmap Image = new Bitmap(ms); ChatBoxAdd("Success!"); MainPicture.MainPictureChange(Image); //pictureBox1Change(Image); Orginal_Image = Image; ms.Close(); DecompressedStream.Close(); ImageServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 0); Client[0].Send(new byte[4]); sw = true; } catch (Exception ex) { ChatBoxAdd(ex.ToString()); ImageServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 0); Client[0].Send(new byte[4]); sw = true; } GetImageBytes = null; //Thread.Sleep(100); GetImageBit = null; GC.Collect(0); GC.Collect(1); } } catch (Exception Ex2) { ChatBoxAdd(Ex2.ToString()); } }