title: Setting up a TCP/IP Client and Server to communicate over a network
body: <p>I am trying to learn a little about socket programming and I have stumbled across TcpListener and TcpClient to use as I read they are slightly easier for beginners.
  The basic jist of what I want to accomplish is to have a small form that can be run on my laptop and another laptop on the same network and for them to be able to communicate i.e. send a string of text to each other. Once I have this I will hopefully develop it further :)</p>
  
  <p>So far I have created both a Client and a Server program using msdn and various guides found on the internet. I can get them to communicate when both running on one laptop however when I move the client to another laptop I get nowhere. I think my main issue is I do not understand quite how the client finds the Servers IP as I think I could hard code it but when I come back at another time I'm sure the IP will have changed. Is there any way to get the two to connect in a more dynamic way to encompass the changing IP?
  My current Client code:</p>
  
  <pre><code>    public void msg(string mesg)
      {
          lstProgress.Items.Add("&gt;&gt; " + mesg);
      }
  
      private void btnConnect_Click(object sender, EventArgs e)
      {
          string message = "Test";
          try
          {
              // Create a TcpClient.
              // Note, for this client to work you need to have a TcpServer 
              // connected to the same address as specified by the server, port
              // combination.
              Int32 port = 1333;
              TcpClient client = new TcpClient(&lt;not sure&gt;, port); //Unsure of IP to use.
  
              // Translate the passed message into ASCII and store it as a Byte array.
              Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
  
              // Get a client stream for reading and writing.
              //  Stream stream = client.GetStream();
  
              NetworkStream stream = client.GetStream();
  
              // Send the message to the connected TcpServer. 
              stream.Write(data, 0, data.Length);
  
              lstProgress.Items.Add(String.Format("Sent: {0}", message));
  
              // Receive the TcpServer.response.
  
              // Buffer to store the response bytes.
              data = new Byte[256];
  
              // String to store the response ASCII representation.
              String responseData = String.Empty;
  
              // Read the first batch of the TcpServer response bytes.
              Int32 bytes = stream.Read(data, 0, data.Length);
              responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
              lstProgress.Items.Add(String.Format("Received: {0}", responseData));
  
              // Close everything.
              stream.Close();
              client.Close();
          }
          catch (ArgumentNullException an)
          {
              lstProgress.Items.Add(String.Format("ArgumentNullException: {0}", an));
          }
          catch (SocketException se)
          {
              lstProgress.Items.Add(String.Format("SocketException: {0}", se));
          }
      }
  </code></pre>
  
  <p>My current Server code:</p>
  
  <pre><code>    private void Prog_Load(object sender, EventArgs e)
      {
          bw.WorkerSupportsCancellation = true;
          bw.WorkerReportsProgress = true;
          bw.DoWork += new DoWorkEventHandler(bw_DoWork);
          bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
  
          if (bw.IsBusy != true)
          {
              bw.RunWorkerAsync();
          }
      }
  
      private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          lstProgress.Items.Add(e.UserState);
      }
  
      private void bw_DoWork(object sender, DoWorkEventArgs e)
      {
          BackgroundWorker worker = sender as BackgroundWorker;
  
          if ((worker.CancellationPending == true))
          {
              e.Cancel = true;
          }
          else
          {
              try
              {
                  // Set the TcpListener on port 1333.
                  Int32 port = 1333;
                  //IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                  TcpListener server = new TcpListener(IPAddress.Any, port);
  
                  // Start listening for client requests.
                  server.Start();
  
                  // Buffer for reading data
                  Byte[] bytes = new Byte[256];
                  String data = null;
  
                  // Enter the listening loop.
                  while (true)
                  {
                      bw.ReportProgress(0, "Waiting for a connection... ");
                      // Perform a blocking call to accept requests.
                      // You could also user server.AcceptSocket() here.
                      TcpClient client = server.AcceptTcpClient();
                      bw.ReportProgress(0, "Connected!");
  
                      data = null;
  
                      // Get a stream object for reading and writing
                      NetworkStream stream = client.GetStream();
  
                      int i;
  
                      // Loop to receive all the data sent by the client.
                      while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                      {
                          // Translate data bytes to a ASCII string.
                          data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                          bw.ReportProgress(0, String.Format("Received: {0}", data));
  
                          // Process the data sent by the client.
                          data = String.Format("I Have Received Your Message: {0}", data);
  
                          byte[] mssg = System.Text.Encoding.ASCII.GetBytes(data);
  
                          // Send back a response.
                          stream.Write(mssg, 0, mssg.Length);
                          bw.ReportProgress(0, String.Format("Sent: {0}", data));
                      }
  
                      // Shutdown and end connection
                      client.Close();
                  }
              }
              catch (SocketException se)
              {
                  bw.ReportProgress(0, String.Format("SocketException: {0}", se));
              }
          }
      }
  </code></pre>
  
  <p>As you can probably tell I am brand new to this so if there is a better way to implement this I am more than happy to learn!
  Thanks for any help in advance :)</p>
  
  <p>My solution thanks to the answers below:</p>
  
  <pre><code>private String IPAddressCheck()
      {
          var IPAddr = Dns.GetHostEntry("HostName");
          IPAddress ipString = null;
  
          foreach (var IP in IPAddr.AddressList)
          {
              if(IPAddress.TryParse(IP.ToString(), out ipString) &amp;&amp; IP.AddressFamily == AddressFamily.InterNetwork)
              {
                  break;
              }
          }
          return ipString.ToString();
      }
  
      private void btnConnect_Click(object sender, EventArgs e)
      {
          string message = "Test";
          try
          {
              Int32 port = 1337;  
              string IPAddr = IPAddressCheck();
              TcpClient client = new TcpClient(IPAddr, port);
  </code></pre>
  
  <p>I'm not sure if it is the neatest solution but it is working well so thank you for the answers :)</p>