title: Why i'm getting timeout when trying to connect from my android device to the web server on my pc?
body: <p>The web server on the pc is in c#
  In the form1 constructor:</p>
  
  <pre><code>var ws = new WebServer(
      request =&gt; Task.Run(() =&gt; SendResponseAsync(request)),
      "http://+:8098/");
      ws.Run();
  </code></pre>
  
  <p>This two methods</p>
  
  <pre><code>public static string GetLocalIPAddress()
          {
              var host = Dns.GetHostEntry(Dns.GetHostName());
              foreach (var ip in host.AddressList)
              {
                  if (ip.AddressFamily == AddressFamily.InterNetwork)
                  {
                      return ip.ToString();
                  }
              }
              throw new Exception("Local IP Address Not Found!");
          }
  
          public void Send(string ipaddress)
          {
              UdpClient client = new UdpClient();
              IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 15000);
              byte[] bytes = Encoding.ASCII.GetBytes(ipaddress);
              client.Send(bytes, bytes.Length, ip);
              client.Close();
          }
  </code></pre>
  
  <p>Then in timer tick event interval set to 1000ms </p>
  
  <pre><code>int countsends = 0;
          private void timer2_Tick(object sender, EventArgs e)
          {
              if (countsends == 10)
              {
                  Send(localipadd);
                  countsends = 0;
              }
              countsends += 1;
          }
  </code></pre>
  
  <p>And the WebServer class</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Net;
  using System.Threading;
  
  namespace Automatic_Record
  {
      class WebServer
      {
          private readonly HttpListener _listener = new HttpListener();
          private readonly Func&lt;HttpListenerRequest, Task&lt;string&gt;&gt; _responderMethod;
  
          public WebServer(string[] prefixes, Func&lt;HttpListenerRequest, Task&lt;string&gt;&gt; method)
          {
              try
              {
                  if (!HttpListener.IsSupported)
                      throw new NotSupportedException(
                          "Needs Windows XP SP2, Server 2003 or later.");
  
                  // URI prefixes are required, for example 
                  // "http://localhost:8080/index/".
                  if (prefixes == null || prefixes.Length == 0)
                      throw new ArgumentException("prefixes");
  
                  // A responder method is required
                  if (method == null)
                      throw new ArgumentException("method");
  
                  foreach (string s in prefixes)
                      _listener.Prefixes.Add(s);
  
                  _responderMethod = method;
                  _listener.Start();
              }
              catch(AccessViolationException err)
              {
                  string error = err.StackTrace;
              }
          }
  }
  </code></pre>
  
  <p>On the pc side the c# i'm not getting any errors or exceptions and using break point i can see the pc ip on the network on the router on the method Send on variable ipaddress it's value is 10.0.0.1 i also logged in to my router settings and i saw that the pc is on 10.0.0.1</p>
  
  <p>Now the java side in the android-studio where i'm trying to get the pc ip and to connect to it:</p>
  
  <p>At the top of mainactivity: ( I tried before port 8098 but it didn't work so i tried 15000 but also didn't work still getting timeout message )</p>
  
  <pre><code>private String[] ipaddresses = new String[]{
              "http://10.0.0.1:15000/?cmd=nothing"
              };
  </code></pre>
  
  <p>Then a button click method calling from onCreate</p>
  
  <pre><code>@Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          addListenerOnButton();
          currentActivity = this;
          initTTS();
      }
  </code></pre>
  
  <p>The addListenerOnButton</p>
  
  <pre><code>public void addListenerOnButton()
      {
  
          btnClick = (Button) findViewById(R.id.connecttoserverbutton);
  
          btnClick.setOnClickListener(new OnClickListener()
          {
              byte[] response = null;
              @Override
              public void onClick(View arg0)
              {
  
                  text = (TextView) findViewById(R.id.statusTextView);
  
                  Thread t = new Thread(new Runnable()
                  {
                      @Override
                      public void run()
                      {
                          for (int i = 0; i &lt; ipaddresses.length; i++)
  
                          {
                              counter = i;
                              try
                              {
                                  response = Get(ipaddresses[i]);
                              }
                              catch (Exception e)
                              {
                                  String err = e.toString();
                              }
  
                              if (response!=null)
                              {
  
  
                                  try
                                  {
                                      final String a = new String(response, "UTF-8");
  
  
  
  
                                      text.post(new Runnable()
                                      {
                                          @Override
                                          public void run()
                                          {
                                              text.setText(a + " Oמ " + ipaddresses[counter]);
                                          }
                                      });
                                      iptouse = ipaddresses[i].substring(0,ipaddresses[i].lastIndexOf("=")+1);
                                      connectedtoipsuccess = true;
                                      Logger.getLogger("MainActivity(inside thread)").info(a);
                                  } catch (UnsupportedEncodingException e)
                                  {
                                      e.printStackTrace();
                                      Logger.getLogger("MainActivity(inside thread)").info("encoding exception");
                                  }
  
                                  Logger.getLogger("MainActivity(inside thread)").info("test1");
                                  break;
  
                              }
                              else
                              {
  
                              }
  
  
  
  
  
                          }
                          counter = 0;
                          if (response == null)
                          {
                              text.post(new Runnable()
                              {
                                  @Override
                                  public void run()
                                  {
                                      text.setText("Connection Failed");
                                  }
                              });
                          }
                      }
                  });
                  t.start();
              }
          });
  
      }
  </code></pre>
  
  <p>And last the Get method</p>
  
  <pre><code>private byte[] Get(String urlIn)
      {
          URL url = null;
          String urlStr = urlIn;
          if (urlIn!=null)
              urlStr=urlIn;
  
          try
          {
              url = new URL(urlStr);
          } catch (MalformedURLException e)
          {
              e.printStackTrace();
              return null;
          }
          HttpURLConnection urlConnection = null;
          try
          {
              urlConnection = (HttpURLConnection) url.openConnection();
  
              InputStream in = new BufferedInputStream(urlConnection.getInputStream());
  
              byte[] buf=new byte[10*1024];
              int szRead = in.read(buf);
              byte[] bufOut;
  
              if (szRead==10*1024)
              {
                  throw new AndroidRuntimeException("the returned data is bigger than 10*1024.. we don't handle it..");
              }
              else
              {
                  bufOut = Arrays.copyOf(buf, szRead);
              }
              return bufOut;
          }
          catch (IOException e)
          {
              e.printStackTrace();
              return null;
          }
          finally
          {
              if (urlConnection!=null)
                  urlConnection.disconnect();
          }
      }
  </code></pre>
  
  <p>My android device is also connected to the same network on the router with wifi i checked on the router settings and i see my device.</p>
  
  <p>I used a break point on the android-studio side inside the Get method.
  It's getting to the line:</p>
  
  <pre><code>InputStream in = new BufferedInputStream(urlConnection.getInputStream());
  </code></pre>
  
  <p>Then i click continue and that's it here it's hanging and instead keep going to the next line <code>byte[] buf=new byte[10*1024];</code> after about 30 seconds it's jumping to:  <code>e.printStackTrace();</code></p>
  
  <p>android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
  failed to connect to /10.0.0.1 (port 15000): connect failed: ETIMEDOUT (Connection timed out)</p>
  
  <p>I can't see any exceptions in the logcat.</p>