title: C# Client/Server returning answer from server to client
body: <p>I've created a simple client/server program which takes an input from the client and returns a answer to the input by looking in the text file  to see if there is an answer associated with the input.</p>
  
  <p>The issue I'm having is that I get the response on the server side but I don't know how to send it back to the client (it just returns the input on the client side). </p>
  
  <p>The second issue is that it will execute once, as in, it will only take in one input. I tried adding a loop but couldn't get it to work.</p>
  
  <p>Server.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Net.Sockets;
  using System.Net;
  
  namespace server
  {
      class server
      {
          const string SERVER_IP = "127.0.0.1";
          static void Main(string[] args)
          {
              //Create Dictionary
              Dictionary&lt;string, string&gt; dict = new Dictionary&lt;string, string&gt;();
              //---Read Text File containing commands ---
              StreamReader sr = new StreamReader(@"C:\Users\Desktop\potato.txt");
              string line;
  
              //Splits the text into commands:responses
              while ((line = sr.ReadLine()) != null)
              {
                  string[] arr = line.Split(';');
                  dict.Add(arr[0], arr[1]);
              }
              //Print dictionary TESTING FUNCTION
              foreach (KeyValuePair&lt;string, string&gt; kvp in dict)
              {
                  Console.WriteLine("Command = {0} Response = {1}", kvp.Key, kvp.Value);
              }
  
              //---Input the port number for clients to conect---
              Console.Write("Input port" + System.Environment.NewLine);
              int PORT_NO = int.Parse(Console.ReadLine());
  
              //---listen at the specified IP and port no.---
              IPAddress localAdd = IPAddress.Parse(SERVER_IP);
              TcpListener listener = new TcpListener(localAdd, PORT_NO);
              Console.WriteLine("Listening for Commands");
              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 command data received into a string---
              string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
              Console.WriteLine("Received Command : " + dataReceived);
  
              //---Search Command and send a response
              string Response;
              if (dict.TryGetValue(dataReceived, out Response))
              {
                  Console.WriteLine(Response);
              }
              //---write back the response to the client---
              Console.WriteLine("Sending Response : " + Response);
              nwStream.Write(buffer, 0, bytesRead);
              Console.ReadLine();
          }
      }
  }
  </code></pre>