title: Sending multiple type of data from a single network stream in C#
body: <p>I am creating a program similar to Microsoft Netmeeting for that I have to send Multiple type of data from a single connection such as Mouse position, Pressed key and offcourse a screen shot at a time. I am success fully sending and receiving Screen shot to the client but I am unable to understand how multiple data could be send through the single connetion.</p>
  
  <p>I think it require multiple ports for this purpose. One for Screen shot, One for Mouse Pos and One for Key pressed.</p>
  
  <p>Following are the codes I am using:
  Server = Sender of ScreenShot, Recever of MousePos and Key.
  Client = Receiver of ScreenShot, Sender of MousePos and Key.</p>
  
  <p>Server:</p>
  
  <pre><code>void StartListen()
  {
      try
      {
          IPEndPoint ipendp = new IPEndPoint(IPAddress.Parse(OwnIP()), 532);
          tcpl = new TcpListener(ipendp);
          tcpl.Start();
          s1 = tcpl.AcceptSocket();
          ns = new NetworkStream(s1);
          timer1.Enabled = true;
          while (true)
          {
              byte[] buffer = imageToByteArray(CaptureScreenShot());
              s1.Send(buffer, buffer.Length, SocketFlags.None);
              Thread.Sleep(250);
          }
      }
      catch
      {
          tcpl.Stop();
          ns.Close();
          //tcpl.EndAcceptSocket();
          Form1_Load(0,EventArgs.Empty);
      }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>void StartClient()
  {
      try
      {
  
          IPEndPoint ipendp = new IPEndPoint(IPAddress.Parse(toolStripTextBox1.Text), 532);
          this.Text = "SWare Application - " + toolStripTextBox1.Text + ":532";
          tcpc = new TcpClient();
          tcpc.Connect(ipendp);
          Socket s1 = tcpc.Client;
          ns = tcpc.GetStream();
  
          while (true)
          {
              byte[] b = new byte[500000];
              s1.Receive(b);
              MemoryStream ms = new MemoryStream(b);
              pictureBox1.Image = Image.FromStream(ms);
              //Thread.Sleep(250);
          }
      }
      catch
      {
          tcpc.Close();
          MessageBox.Show("Disconnected from the Remote PC");
      }
  }
  </code></pre>