title: c# Sockets Programming Errors
body: <p>Hello im trying to create a connection which connect two pc. In local code the below is working without any trouble but  when we starts the clients code in different pc, we cant connect. The code is below. where is the mistake?</p>
  
  <p>the client side:</p>
  
  <pre><code>void Start()
  {
      Debug.Log(string.Format("Starting TCP and UDP clients on port {0}...", 90));
      udpThread = new Thread(new ThreadStart(ClientThread));
      udpThread.Start();
  
  }
  
  void ClientThread()
      {
          try
          {
  
              // Establish the remote endpoint  
              // for the socket. This example  
              // uses port 11111 on the local  
              // computer. 
              IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
  
              string strHostName = "";
              strHostName = System.Net.Dns.GetHostName();
  
              IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
  
              IPAddress addr = ipEntry.AddressList[0];
              IPAddress addr2 = ipEntry.AddressList[1];
  
  
  
              IPEndPoint localEndPoint = new IPEndPoint(addr, 90);
  
              // Creation TCP/IP Socket using  
              // Socket Class Costructor 
              Socket sender = new Socket(addr.AddressFamily,
                         SocketType.Stream, ProtocolType.Tcp);
  
              try
              {
                  sender.Connect(localEndPoint);
  
  
                  Console.WriteLine("Socket connected to -&gt; {0} ",
                                sender.RemoteEndPoint.ToString());
  
                  byte[] messageSent = Encoding.ASCII.GetBytes("Test Client&lt;EOF&gt;");
                  int byteSent = sender.Send(messageSent);
  
                  byte[] messageReceived = new byte[1024];
  
                  int byteRecv = sender.Receive(messageReceived);
                  Console.WriteLine("Message from Server -&gt; {0}",
                        Encoding.ASCII.GetString(messageReceived,
                                                   0, byteRecv));
  
                  sender.Shutdown(SocketShutdown.Both);
                  sender.Close();
              }
  
              catch (ArgumentNullException ane)
              {
  
                  Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
              }
  
              catch (SocketException se)
              {
  
                  Console.WriteLine("SocketException : {0}", se.ToString());
              }
  
              catch (Exception e)
              {
                  Console.WriteLine("Unexpected exception : {0}", e.ToString());
              }
          }
  
          catch (Exception e)
          {
  
              Console.WriteLine(e.ToString());
          }
  
      }
  </code></pre>
  
  <p>And server side</p>
  
  <pre><code>    private void Form1_Load(object sender, EventArgs e)
      {
          Console.WriteLine(string.Format("Starting TCP and UDP servers on port {0}...", port));
  
          devices_battery_midlow_pic_1.Visible = false;
          devices_battery_low_pic_1.Visible = false;
          devices_battery_midhigh_pic_1.Visible = false;
          devices_battery_high_pic_1.Visible = false;
  
          play_button.Anchor = (AnchorStyles.Bottom);
  
          Console.WriteLine(DateTime.Now.ToString() + " Getting IP...");
          IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
          IPAddress ipAddress = ipHostInfo.AddressList[0];
          connection_id_label.Text = "This IP:\n" + ipAddress.MapToIPv4().ToString();
  
          Console.WriteLine(DateTime.Now.ToString() + " Starting Connection Thread...");
  
          connectionThread = new Thread(new ThreadStart(StartServer));
          connectionThread.IsBackground = true;
          connectionThread.Start();
  
  
      }
      private void StartServer()
      {
          string strHostName = "";
          strHostName = System.Net.Dns.GetHostName();
  
          IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
  
          IPAddress addr = ipEntry.AddressList[0];
  
          Console.WriteLine(addr.MapToIPv4().ToString() + "  Starting Connection Thread...");
  
          IPEndPoint localEndPoint = new IPEndPoint(addr, 90);
  
          Socket listener = new Socket(addr.AddressFamily,
                       SocketType.Stream, ProtocolType.Tcp);
  
          try
          {
              listener.Bind(localEndPoint);
  
              listener.Listen(10);
  
              while (true)
              {
  
                  Console.WriteLine("Waiting connection ... ");
  
                  Socket clientSocket = listener.Accept();
  
                  // Data buffer 
                  byte[] bytes = new Byte[1024];
                  string data = null;
  
  
                  Console.WriteLine("Text received -&gt; {0} ", data);
                  byte[] message = Encoding.ASCII.GetBytes("Test Server");
  
                  clientSocket.Send(message);
  
                  clientSocket.Shutdown(SocketShutdown.Both);
                  clientSocket.Close();
              }
          }
  
          catch (Exception e)
          {
              Console.WriteLine(e.ToString());
          }
      }
  </code></pre>
  
  <p>That is working on one pc, but if we move our server code to other pc, we cant take any response. Where is error??</p>