title: Sending a value from server to client with sockets
body: <p>I am using the following projects in order to create an asynchronous communication between <a href="https://msdn.microsoft.com/en-us/library/fx6588te%28v=vs.110%29.aspx" rel="noreferrer">server</a> and <a href="https://msdn.microsoft.com/en-us/library/bew39x2a(v=vs.110).aspx" rel="noreferrer">client</a> sockets. When I am running those projects I am sending a message from the client to the server thus I got the message: </p>
  
  <blockquote>
    <p>Data : recording EOF, Sent 14 bytes to client.</p>
  </blockquote>
  
  <p>What I want to achieve is to send a boolean variable from the server to the client with the sockets. Is it doable to do so, I am wondering since in the code I have the server which waiting and listens and the client which sends data, can I do the opposite? In general what I want is to send a boolean to several clients. Why am I need the End of File in order to send a string? Is it necessary to convert everything to string? </p>
  
  <p><strong>EDIT</strong>: In general what I want is to send a variable from one computer to two others in order a process to begin simultaneously in all computers. In fact to create a switcher that gives a signal to begin a process in 2-3 machines in the same time.</p>
  
  <p>I tried to use the following code for <strong>server</strong>:</p>
  
  <pre><code>class Program
  {
      const int PORT_NO = 2201;
      const string SERVER_IP = "127.0.0.1";
      static void Main(string[] args)
      {
          //---listen at the specified IP and port no.---
          IPAddress localAdd = IPAddress.Parse(SERVER_IP);
          TcpListener listener = new TcpListener(localAdd, PORT_NO);
          Console.WriteLine("Listening...");
          listener.Start();
          //---incoming client connected---
          TcpClient client = listener.AcceptTcpClient();
          //---get the incoming data through a network stream---
          NetworkStream nwStream = client.GetStream();
          byte[] buffer = new byte[client.ReceiveBufferSize];
          //---read incoming stream---
          int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
          //---convert the data received into a string---
          string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
          Console.WriteLine("Received : " + dataReceived);
          //---write back the text to the client---
          Console.WriteLine("Sending back : " + dataReceived);
          nwStream.Write(buffer, 0, bytesRead);
          client.Close();
          listener.Stop();
          Console.ReadLine();
      }
  }
  </code></pre>
  
  <p>and for <strong>client</strong>:</p>
  
  <pre><code>class Program
  {
      const int PORT_NO = 2201;
      const string SERVER_IP = "127.0.0.1";
      static void Main(string[] args)
      {
          //---data to send to the server---
          string textToSend = DateTime.Now.ToString();
          //---create a TCPClient object at the IP and port no.---
          TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
          NetworkStream nwStream = client.GetStream();
          byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);
          //---send the text---
          Console.WriteLine("Sending : " + textToSend);
          nwStream.Write(bytesToSend, 0, bytesToSend.Length);
          //---read back the text---
          byte[] bytesToRead = new byte[client.ReceiveBufferSize];
          int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
          Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
          Console.ReadLine();
          client.Close();
      }
  }
  </code></pre>
  
  <p>Just in the case I am working in the same machine. I will have in total 4 machines and I want the one of them to give a singal to the rest of them to begin recording rgb stream. Thus the server should send signal to the clients to begin recording. What should I do to change the behavior of the server to send data and not listen. Is it possible to have several machines listening and waiting for a signal to be given?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>private void mouseClick1(object sender, MouseEventArgs e)
      {
  
          Thread thread = new Thread(() =&gt; StartServer());
          thread.Start();
  
          if (e.Button == MouseButtons.Left)
          {
              button5.Enabled = false;
              button3.Enabled = true;
  
              try
              {
                  obj = new Capturer();
              }
              catch (Exception e1)
              {
                  Console.WriteLine("The process failed: {0}", e1.ToString());
              }
          }
      }
  
      private void mouseClick2(object sender, MouseEventArgs e)
      {
          if (e.Button == MouseButtons.Right)
          {
              obj.flag2 = true;
          }
      }
  </code></pre>
  
  <p>My code as it is now with left click calls startServer() function with a new thread which is the main code in @Ians implementation and afterthat I call my object. When I click right click I change a flag and the capturer stops. How can I stop also the server or pause in order to open it again with the left click?</p>