title: C# TCP Client to automatically connect to server listening on specific port
body: <p>I seem to be having a hard time figuring out how to get my client app to automatically connect to a server app running on a separate machine on my LAN.</p>
  
  <p>Right now the only way I'm able to get the client to connect to the server is by manually specifying the server's IP address in code:</p>
  
  <pre><code> private TcpClient client = new TcpClient();
   private IPEndPoint serverEndPoint = neIPEndPoint(IPAddress.Parse("Server IP address goes here"), 8888);
  </code></pre>
  
  <p>My server app uses a TCP Listener, so I figured my client could do something similar, to be able to find the server, but I can't figure out how to implement it in code.</p>
  
  <p>Code from my server app for finding the client to connect to:</p>
  
  <pre><code>private TcpListener tcpListener;
      private Thread listenThread;
      private int connectedClients = 0;
      private delegate void WriteMessageDelegate(string msg);
  
      public Form1()
      {
          InitializeComponent();
          Server();
      }
  
      private void Server()
      {
          this.tcpListener = new TcpListener(IPAddress.Any, 8888); 
          this.listenThread = new Thread(new ThreadStart(ListenForClients));
          this.listenThread.Start();
      }
  </code></pre>
  
  <p>I've tried using a TextBox that the user can manually enter their server's IP address into (since they wouldn't have access to the code), but I think an automatic connection would be much more user friendly, especially since I don't know how to permanently save the user's server IP address if they use the above method of setting the IP for the client to connect to.</p>
  
  <p>So, my question is: What would be the best method for me to enable my client to automatically connect to a server running on my LAN?</p>
  
  <p>Thanks,</p>
  
  <p>Patrick</p>
  
  <hr>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I tried implementing the code for a UDP broadcast, but I can't seem to get it working.</p>
  
  <p>Here is what I've added to my client (Along with the client code I had in there before):</p>
  
  <pre><code>public partial class Console : Form
  {
  
      //FIND SERVER
  
          private void FindServer()
      {
          var Client = new UdpClient();
          var RequestData = Encoding.ASCII.GetBytes("SomeRequestData");
          var ServerEp = new IPEndPoint(IPAddress.Any, 0);
  
          Client.EnableBroadcast = true;
          Client.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));
  
          var ServerResponseData = Client.Receive(ref ServerEp);
          var ServerResponse = Encoding.ASCII.GetString(ServerResponseData);
          Console.WriteLine("Recived {0} from {1}", ServerResponse, ServerEp.Address.ToString());
  
          Client.Close();
      }
  
  
      // SEND MESSAGES TO SERVER (VIA USER INTERACTION)
  
      private TcpClient client = new TcpClient();
      private IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("My IP Address was here (I tried changing it to "broadcast" as well"), 8888);
  
      public Console()
      {
          InitializeComponent();
          client.Connect(serverEndPoint);
  
      }
  
      private void SendMessage(string msg)
      {
          NetworkStream clientStream = client.GetStream();
  
          ASCIIEncoding encoder = new ASCIIEncoding();
          byte[] buffer = encoder.GetBytes(msg);
  
          clientStream.Write(buffer, 0, buffer.Length);
          clientStream.Flush();
  
      }
  </code></pre>
  
  <p>Here is what I've added to my server code:</p>
  
  <pre><code>private void BroadcastToClients()
      {
          var Server = new UdpClient(8888);
          var ResponseData = Encoding.ASCII.GetBytes("SomeResponseData");
  
          while (true)
          {
              var ClientEp = new IPEndPoint(IPAddress.Any, 0);
              var ClientRequestData = Server.Receive(ref ClientEp);
              var ClientRequest = Encoding.ASCII.GetString(ClientRequestData);
  
              Console.WriteLine("Recived {0} from {1}, sending response", ClientRequest, ClientEp.Address.ToString());
              Server.Send(ResponseData, ResponseData.Length, ClientEp);
          }
      }
  </code></pre>
  
  <p>I'd imagine there must be some conflicting code in there, but since I'm so new to this, I can't seem to figure it out...</p>
  
  <hr>
  
  <hr>
  
  <p><strong>UPDATE</strong>
  I've still not managed to make any progress on this. Anyone out there that might be able to chime in and help me figure out why this isn't working for me?</p>