title: Difference of C# TcpListener & Java ServerSocket
body: <p>I'm working on file transfer program between PC &amp; Android Device. but I found a problem in PC server part. </p>
  
  <p>First, my android app request connection to PC like below.</p>
  
  <pre><code>public void run() {
      try {
          InetAddress lServerAddress = InetAddress.getByAddress(
              new byte[] { (byte)192, (byte)168, (byte)0, (byte)196 });
          Socket lClient = new Socket();
          lClient.bind(null);
          lClient.connect(new InetSocketAddress(lServerAddress, 12303), 20000);
          OutputStream lOutput = lClient.getOutputStream();
          lOutput.write(ByteBuffer.allocate(4).putInt(123456).array());
      } catch (Exception e) {
          Log.e(AppManager.msLogTag, e.getMessage());
      }
  }
  </code></pre>
  
  <p>This codes are executed in worker thread. </p>
  
  <p>And then server codes in Java.</p>
  
  <pre><code>public static void main(String[] args) {
      try {
          ServerSocket lServer = new ServerSocket(12303);
          Socket lClient = lServer.accept();
          System.out.println("Accepted.");
          InputStream lReadStream = lClient.getInputStream();
          byte[] lBuffer = new byte[4];
          lReadStream.read(lBuffer, 0, lBuffer.length);
          ByteBuffer lBytes = ByteBuffer.wrap(lBuffer);
          System.out.println(lBytes.getInt());
          lReadStream.close();
          lClient.close();
          lServer.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>It works perfectly. </p>
  
  <p>However in C#, <code>TcpListener</code> do not accept client forever. Here is source.</p>
  
  <pre><code>static void Main(string[] args)
  {
      IPAddress lServerIp = null;
      foreach (IPAddress lIp in Dns.GetHostAddresses(Dns.GetHostName()))
      {
          if (lIp.AddressFamily == AddressFamily.InterNetwork)
          {
              lServerIp = lIp;
              break;
          }
      }
      if (lServerIp != null)
      {
          TcpListener lServer = new TcpListener(lServerIp, 12303);
          Console.WriteLine(lServerIp.ToString());
          lServer.Start();
          using (Socket lClient = lServer.AcceptSocket())
          {
              Console.WriteLine(((IPEndPoint)lClient.RemoteEndPoint).Address.ToString());
              using (Stream lStream = new NetworkStream(lClient))
              {
                  byte[] lBuffer = new byte[4];
                  lStream.Read(lBuffer, 0, lBuffer.Length);
                  int lHead = BitConverter.ToInt32(lBuffer, 0);
                  Console.WriteLine(lHead.ToString());
              }
          }
      }
  }
  </code></pre>
  
  <p>Environments are same, port forwarded and registered firewall exception. So I think, Java's <code>ServerSocket</code> and C#'s <code>TcpListener</code> are working differently. However, TCProtocol has world standard, BTW how can they working differently? I cannot understand this situation.</p>
  
  <p>Finally, how can I fix C# server?</p>
  
  <p>Thanks.</p>