title: Send video frame via socket
body: <p>I captured video stream with Kinect device. I need to Send this streams over network. For this purpose I used sockets.
  Her is piece of code in server and client:</p>
  
  <pre><code>private System.Drawing.Bitmap _CurrentBitmap;
      public ManWindow()
      {
          InitializeComponent();
  
          this.Loaded += delegate
          {
              BackgroundWorker bw1 = new BackgroundWorker();
              bw1.RunWorkerCompleted += (a, b) =&gt; bw1.RunWorkerAsync();
              bw1.DoWork += delegate { SendImage(); };
              bw1.RunWorkerAsync();
          };
      }
      public void SendImage()
      {
          Socket sListen = new Socket(AddressFamily.InterNetwork,
                                      SocketType.Stream,
                                      ProtocolType.Tcp);
  
          IPAddress IP = IPAddress.Parse("127.0.0.1");
          IPEndPoint IPE = new IPEndPoint(IP, 4321);
  
          sListen.Bind(IPE);
          sListen.Listen(2);
  
          while (true)
          {
              Socket clientSocket;
              clientSocket = sListen.Accept();
  
              var converter = new System.Drawing.ImageConverter();
              byte[] buffer = (byte[])converter.ConvertTo(_CurrentBitmap, typeof(byte[]));
              clientSocket.Send(buffer, buffer.Length, SocketFlags.None);
          }
      }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>    private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          BackgroundWorker bw11111 = new BackgroundWorker();
          bw11111.RunWorkerCompleted += (a, b) =&gt; bw11111.RunWorkerAsync();
          bw11111.DoWork += delegate { Recive(); };
          bw11111.RunWorkerAsync();
          Recive();
      }
  
      public void Recive()
      {
          Socket s = new Socket(AddressFamily.InterNetwork,
                              SocketType.Stream,
                              ProtocolType.Tcp);
  
          IPAddress IP = IPAddress.Parse("127.0.0.1");
          IPEndPoint IPE = new IPEndPoint(IP, 4321);
          s.Connect(IPE);
  
          while (true)
          {
  
              byte[] buffer = new byte[1000000];
  
              s.Receive(buffer, buffer.Length, SocketFlags.None);
  
              MemoryStream ms = new MemoryStream(buffer);
  
              ms.Write(buffer, 0, buffer.Length);
              System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ms);
  
              Dispatcher.BeginInvoke(new Action(() =&gt;
              {
                  rgbImage11.Source = bitmap.ToBitmapSource();
              }));
  
          }
      }
  </code></pre>
  
  <p>_CurrentBitmap is frame that readed from Kinect and every 1 second updated. When Client starts, call Socket Receive method, it receives frame and after convert to bitmap asign it to Image WPF control. My problem is this action occurs ones and loop in client not repeated.</p>