title: C# Server and Java Client
body: <p>I'm trying to write a small client server program. The Server is in C#, and the client is in Java.
  Here are the codes:</p>
  
  <p>Server:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net;
  using System.Net.Sockets;
  using System.IO;
  
  namespace Server
  {
      class Program
      {
          private TcpListener tcpListener;
          public static void Main(string[] args)
          {
              Program program = new Program();
              program.StartServer();
  
              while (true) ;
          }
  
          private bool StartServer()
          {
              IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
  
              try
              {
                  tcpListener = new TcpListener(ipAddress, 5678);
                  tcpListener.Start();
                  tcpListener.BeginAcceptTcpClient(new AsyncCallback(this.ProcessEvents), tcpListener);
  
                  Console.WriteLine("Listing at Port {0}.", 5678);
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.ToString());
                  return false;
              }
  
              return true;
          }
  
          private void ProcessEvents(IAsyncResult asyn)
          {
              try
              {
                  TcpListener processListen = (TcpListener)asyn.AsyncState;
                  TcpClient tcpClient = processListen.EndAcceptTcpClient(asyn);
                  NetworkStream myStream = tcpClient.GetStream();
                  if (myStream.CanRead)
                  {
                      StreamReader readerStream = new StreamReader(myStream);
                      string myMessage = readerStream.ReadToEnd();
                      readerStream.Close();
                  }
                  myStream.Close();
                  tcpClient.Close();
                  tcpListener.BeginAcceptTcpClient(new AsyncCallback(this.ProcessEvents), tcpListener);
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.ToString());
              }
          }
      }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>import java.io.PrintWriter;
  import java.net.Socket;
  
  
  public class Program {
  
      public static void main(String[] args) {
          Socket socket;
          try {
              socket = new Socket( "127.0.0.1", 5678);
              PrintWriter writer = new PrintWriter(socket.getOutputStream());
              writer.print("Hello world");
              writer.flush();
              writer.close();
              socket.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  
  }
  </code></pre>
  
  <p>But when I try to create a Socket in client, I get this exception:</p>
  
  <pre><code>java.net.ConnectException: Connection refused: connect
  at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
  at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
  at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
  at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
  at java.net.PlainSocketImpl.connect(Unknown Source)
  at java.net.SocksSocketImpl.connect(Unknown Source)
  at java.net.Socket.connect(Unknown Source)
  at java.net.Socket.connect(Unknown Source)
  at java.net.Socket.&lt;init&gt;(Unknown Source)
  at java.net.Socket.&lt;init&gt;(Unknown Source)
  at Program.main(Program.java:10)
  </code></pre>
  
  <p>Can anyone tell me what I'm doing wrong here?</p>
  
  <p>updated: I'm running x64 Windows 7 Ultimate, I don't see anything firewall message pop up (I did saw it for server once, which I allowed and set to always allow). I can connect using telenet, no problem with that. Any other suggestion please.</p>