title: How To Send Background Worker Information To Another Computer In C#?
body: <p>So I am writing a client/server program, which the client will listen for a command from the server program. I have the two able to find each other on the network with their ip addresses. Essentially, the server program has a background worker with code that I want to send to the client machine to run. Is there a way to do this?</p>
  <p>Server:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.IO;
  using System.Net;
  using System.Net.Sockets;
  using System.Text;
  using System.Windows.Forms;
  
  namespace AILocker
  {
      public partial class HiveMind : Form
      {
          public HiveMind()
          {
              InitializeComponent();
          }
  
          private void Connect_Click(object sender, EventArgs e)
          {
  
              {
                  try
                  {
                      IPAddress ipAd = IPAddress.Parse(ipv4.Text); //use local m/c IP address, and use the same in the client
  
                      /* Initializes the Listener */
                      TcpListener myList = new TcpListener(ipAd, 8001);
  
                      /* Start Listeneting at the specified port */
                      myList.Start();
  
                      MessageBox.Show(&quot;The server is running at port 8001...&quot;);
                      MessageBox.Show(&quot;The local End point is  :&quot; + myList.LocalEndpoint);
                      MessageBox.Show(&quot;Looking for other computer&quot;);
  
                      Socket s = myList.AcceptSocket();
                      Console.WriteLine(&quot;Found buddy &quot; + s.RemoteEndPoint);
  
                      byte[] b = new byte[100];
                      int k = s.Receive(b);
                      Console.WriteLine(&quot;Recieved...&quot;);
                      for (int i = 0; i &lt; k; i++)
                          Console.Write(Convert.ToChar(b[i]));
  
                      ASCIIEncoding asen = new ASCIIEncoding();
                      s.Send(asen.GetBytes(&quot;The string was recieved by the server.&quot;));
                      s.Send(MainForm.googlesearch.RunWorkerAsync());
                      Console.WriteLine(&quot;\nSent Acknowledgement&quot;);
                      /* clean up */
                      
                      s.Close();
                      myList.Stop();
  
                  }
                  catch (Exception)
                  {
                      MessageBox.Show(&quot;Could not find other computer.&quot;);
                  }
  
              }
  
          }
  
          private void ipv4_TextChanged(object sender, EventArgs e)
          {
  
          }
      }
  }
  </code></pre>
  <p>Client:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.IO;
  using System.Linq;
  using System.Net.Sockets;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace Listener
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void SL_Click(object sender, EventArgs e)
          {
              try
              {
                  TcpClient tcpclnt = new TcpClient();
                  Console.WriteLine(&quot;Connecting.....&quot;);
  
                  tcpclnt.Connect(RecieveIP.Text, 8001); // use the ipaddress as in the server program
  
                  MessageBox.Show(&quot;Connected&quot;);
                  Console.Write(&quot;Enter the string to be transmitted : &quot;);
  
                  String str = Console.ReadLine();
                  Stream stm = tcpclnt.GetStream();
  
                  ASCIIEncoding asen = new ASCIIEncoding();
                  byte[] ba = asen.GetBytes(str);
                  MessageBox.Show(&quot;Transmitting.....&quot;);
  
                  stm.Write(ba, 0, ba.Length);
  
                  byte[] bb = new byte[100];
                  int k = stm.Read(bb, 0, 100);
  
                  for (int i = 0; i &lt; k; i++)
                      Console.Write(Convert.ToChar(bb[i]));
  
                  tcpclnt.Close();
              }
  
              catch (Exception)
              {
                  MessageBox.Show(&quot;Could not find server computer.&quot;);
              }
          }
  
          private void RecieveIP_TextChanged(object sender, EventArgs e)
          {
  
          }
      }
  }
      
  </code></pre>
  <p>The issue running into is that googlesearch.runworkerasync is unsendable due to its protection level, is there a way to send the code of the backroundworker listed above to be run by the client computer? Thank you.</p>