private static void ConnectTo(string Ip) { try { ConsoleLogic.WriteConsole("Connecting to " + Ip); Socket connectionSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IAsyncResult result = connectionSocket.BeginConnect(new IPEndPoint( IPAddress.Parse(Ip), 6897), null, null); bool connected = result.AsyncWaitHandle.WaitOne(1000, true); if (!connected) { throw new Exception(); } InitScreen(); connectionSocket.Send(Serializer.ObjectToBytes(new ConnectionRequest(localIP.ToString()))); //listen byte[] data = new byte[connectionSocket.ReceiveBufferSize]; int bytes = 0; do { bytes = connectionSocket.Receive(data); } while (connectionSocket.Available > 0); object obj = Serializer.BytesToObj(data, bytes); if (obj is ConnectionResponse) { } } catch (Exception e) { //main.ipVBox.Remove((Gtk.Button)sender); //main.ShowAll(); ConsoleLogic.WriteConsole("Lost connection with " + Ip, e); } }
public static void Main(string[] args) { Application.Init(); MainWindow win = new MainWindow(); NetworkLogic.GetIPVBoxRef(win); ConsoleLogic.SendLabelRef(win.consoleTxt); ConsoleLogic.SendMainWindowRef(win); win.Show(); Application.Run(); }
private static void CommandListenLoop() { try { listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(localIP, 6897)); listener.Listen(10); BinaryFormatter formatter = new BinaryFormatter(); while (true) { Thread.Sleep(10); incomingConnection = listener.Accept(); int bytes = 0; byte[] data = new byte[incomingConnection.ReceiveBufferSize]; do { bytes = incomingConnection.Receive(data); } while (incomingConnection.Available > 0); object obj = Serializer.BytesToObj(data, bytes); if (obj is SearchRequest) { incomingConnection.Send(Serializer.ObjectToBytes(new SearchResponse(localIP.ToString(), Dns.GetHostName()))); } if (obj is ConnectionRequest) { ConsoleLogic.WriteConsole("Connection request from " + (obj as ConnectionRequest).IPAdress); incomingConnection.Send(Serializer.ObjectToBytes(new ConnectionResponse())); Task thr1 = new Task(() => Stream((obj as ConnectionRequest).IPAdress)); thr1.Start(); } } } catch (Exception e) { ConsoleLogic.WriteConsole("error at CommandListenLoop", e); } }
private static void Stream(string ip) { Thread.Sleep(300); int miliseconds = DateTime.Now.Millisecond; pipe = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); bool connected = false; while (!connected) { try { IAsyncResult result = pipe.BeginConnect(new IPEndPoint( IPAddress.Parse(ip), 4578), null, null); connected = result.AsyncWaitHandle.WaitOne(3000, true); } catch (Exception e) { ConsoleLogic.WriteConsole("error at connecting back to receiver"); } } //performSearch = false; int cyclesCount = 0; int fps = 0; int sleepTime = 0; System.Timers.Timer timer = new System.Timers.Timer(1000); timer.Elapsed += (sender, args) => { main.cycleSpeedSend.Text = cyclesCount.ToString(); fps = cyclesCount; sleepTime = (int)(1000 / (((main.FPS.Value - fps) / 60) * 1000)); main.thrSleepTime.Text = sleepTime.ToString(); cyclesCount = 0; }; timer.Enabled = true; Bitmap prevImage = null; while (true) { try { cyclesCount++; //while((DateTime.Now.Millisecond - miliseconds) <= 1000/main.FPS.Value) //{ // Thread.Sleep(1); //} //miliseconds = DateTime.Now.Millisecond; if (fps < main.FPS.Value) { if (main.FPS.Value - fps > 20) { } else { Thread.Sleep((int)(1000 / (((main.FPS.Value - fps) / 60) * 1000))); } } else { } if (!pipe.Connected) { //searchTask.Start(); return; } int sqrSize = (int)main.strImgSize.Value; Bitmap memoryImage = new Bitmap(sqrSize, sqrSize /*, PixelFormat.Format8bppIndexed*/); Size s = new Size(sqrSize, sqrSize); Graphics memoryGraphics = Graphics.FromImage(memoryImage); memoryGraphics.CopyFromScreen(0, 0, 0, 0, s); //Comparison if (prevImage != null) { if (prevImage.Height == memoryImage.Height) { Bitmap prevHolder = memoryImage.Clone() as Bitmap; memoryImage = GetDifferences(prevImage, memoryImage, Color.Pink); prevImage.Dispose(); prevImage = prevHolder; //memoryImage.MakeTransparent(Color.Pink); } else { prevImage = memoryImage.Clone() as Bitmap; } } else { prevImage = memoryImage.Clone() as Bitmap; } //Compress using (var stream = new MemoryStream()) { EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, (long)main.strImgCompression.Value); EncoderParameter colorDepthParam = new EncoderParameter(Encoder.ColorDepth, (long)main.strImgCD.Value); EncoderParameter compressionParam = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionLZW); ImageCodecInfo imageCodec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(o => o.FormatID == ImageFormat.Jpeg.Guid); EncoderParameters parameters = new EncoderParameters(3); parameters.Param[0] = qualityParam; parameters.Param[1] = colorDepthParam; parameters.Param[2] = compressionParam; // memoryImage.Save(stream, imageCodec, parameters); byte[] compressedData = new byte[LZ4Codec.MaximumOutputSize((int)stream.Length)]; LZ4Codec.Encode(stream.GetBuffer(), compressedData, LZ4Level.L12_MAX); main.dataAmount.Text = stream.Length.ToString(); main.compressedDataAmount.Text = compressedData.Length.ToString(); if (compressedData.Length > 15000) { continue; } pipe.Send(Serializer.ObjectToBytes(new ImageStreamPart(compressedData, stream.Length))); } //using (var stream = new MemoryStream()) //{ // compressedImg.Save(stream, ImageFormat.Jpeg); //} memoryImage.Dispose(); memoryGraphics.Dispose(); //Thread.Sleep(30); //Fix large bitmap size //Serializer.ObjectToBytes( // new ImageConverter().ConvertTo( // memoryImage, // typeof(byte[]))) } catch (Exception e) { ConsoleLogic.WriteConsole("Error at sending image", e); } } }
private static void DataListenLoop() { try { int cyclesCount = 0; System.Timers.Timer timer = new System.Timers.Timer(1000); timer.Elapsed += (sender, args) => { main.cycleSpeedReceive.Text = cyclesCount.ToString(); cyclesCount = 0; }; timer.Enabled = true; while (true) { Socket pipeListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); pipeListener.Bind(new IPEndPoint(localIP, 4578)); pipeListener.Listen(10); pipe = pipeListener.Accept(); //int startTime = DateTime.Now.Millisecond; while (pipe.Connected) { cyclesCount++; //main.cycleSpeedReceive.Text = (DateTime.Now.Millisecond - startTime).ToString(); //startTime = DateTime.Now.Millisecond; //Thread.Sleep(10); int bytes = 0; byte[] data = new byte[30000]; do { bytes = pipe.Receive(data); } while (pipe.Available > 0); try { object imgData = Serializer.BytesToObj(data, bytes); if (!(imgData is ImageStreamPart)) { throw new Exception("Wrong data!"); } data = (imgData as ImageStreamPart).bitmap; //decompressing byte[] decompressedImage = new byte[(imgData as ImageStreamPart).originalSize]; LZ4Codec.Decode(data, decompressedImage); strWin.img.Pixbuf = new Gdk.Pixbuf(decompressedImage); if (disconnectRequest) { pipe.Disconnect(true); pipe.Dispose(); pipe = null; discBtn.Hide(); discBtn.Destroy(); discBtn.Dispose(); disconnectRequest = false; strWin.Hide(); strWin.Dispose(); strWin = null; break; //searchTask.Start(); } } catch (Exception e1) { ConsoleLogic.WriteConsole("Error at converting received data"); } } } } catch (Exception e) { ConsoleLogic.WriteConsole("error at DataListenLoop"); } }