0
votes

The C#-Application is the TCP-Server and Delphi-Application represents the TCP-Client polling Server for information.

I'm using Microsoft Visual Studio Community 2017 and .NET-Framework 4.5.1 for the C#-Application and Delphi 7 (Embarcadero) for the Client-Application.

Both Applications run on the same PC, therefore I set IP-Address to "localhost" or "127.0.0.1". The Port is 12345.

class Program
{
  static void Main(string[] args)
  {
    int port = 0;
    IPHostEntry host;
    IPAddress localAddress;          
    try
    {
      port = 12345;
      host = Dns.GetHostEntry("localhost");
      localAddress = host.AddressList[0];
      Console.WriteLine("Server waits for a client");
      // init Listener
      TcpListener listener = new TcpListener(localAddress, port);
      // start Listener 
      listener.Start();
      // Wait for a client..
      TcpClient c = listener.AcceptTcpClient();                    
      Console.WriteLine("Connection established.");                    
      //..                    
      // Close connection
      c.Close();
      // stop Listener
      listener.Stop();
    }
    catch (Exception e)
    {
      Console.WriteLine("Error: " + e.Message);
    }     
  }
}

My Problem is, that I get error # 10061 "Connection refused" in the Client (Delphi) Application when the socket tries to connect to the server.

TForm1 = class(TForm)
  TcpClient1: TTcpClient;
  btnConnect: TButton;
// ..
procedure TForm1.btnConnectClick(Sender: TObject);
begin
  // try to connect to TCP-Server
  TcpClient1.RemoteHost := 'localhost';
  TcpClient1.RemotePort := '12345';
  if not TcpClient1.Connect() then // Error 10061
  begin
    ShowMessage('No connection to server');
  end
  else
  begin
    ShowMessage('Connected with server.');
  end;
end;

In Delphi I tried the component TcpClient as well as the Indy component TIdTCPClient - the error appears on both.

For testing purpose I wrote a Client-Application in C# where the error didn't occur (same address and port).

class Program
{
  static void Main(string[] args)
  {
    try
    {  
      // init Client
      TcpClient c = new TcpClient("localhost", 12345);                    
      Console.WriteLine("Connected to localhost.");
      // ..
      // close connection
      Console.WriteLine("Close connection.");
      c.Close();                               
    }
    catch (Exception e)
    {
      Console.WriteLine("Error: " + e.Message);
    }
    Console.WriteLine("Press Enter to exit... ");
    string cmd = Console.ReadLine();
  }
}

My guess is, that there is something between C# and Delphi, but I have no idea what.

Did anyone has an idea why I cannot establish a connection with the Delphi-Application?


Update: It seems to work, when the C#-Server accepts Clients from any IP-Address.

// ..
// host = Dns.GetHostEntry("localhost");
// localAddress = host.AddressList[0];
Console.WriteLine("Server waits for a client");
// init Listener
TcpListener listener = new TcpListener(IPAddress.Any, port);
//..
1
Can yuo post the code for the C# server?Pete Stensønes
I added code for C# server and C# client to my question above.topet
I was hoping for a little more code to see how the server side is listening (as in the MSDN docs sample on msdn.microsoft.com/en-us/library/…).Pete Stensønes
Does the code as posted (binding to IPAddress.Any) work? The commented-out section that binds to the FIRST address in the DNS entry for localhost may well be an IPV6 address rather than an IPV4, it is on my Windows 8, 8.1 and 10 machines. Delphi sockets (at least in Version 6) do not do IPV6. .NET does.Pete Stensønes
Yes, the code with IPAddress.Any works.topet

1 Answers

0
votes

In C# localAddress can be of Address Family IPv4, IPv6 or others, I wasn't able to connect with my Delphi-Application that works with IPv4. My current solution: I loop through the AddressList and make sure that I get an IP-Address of IPv4-Family.

host = Dns.GetHostEntry("localhost");                
for (int i = 0; i <= host.AddressList.Length - 1; i++)
{
  if (host.AddressList[i].AddressFamily == AddressFamily.InterNetwork) // check if family is IPv4 
  {
    localAddress = host.AddressList[i];
    // init Listener
    listener = new TcpListener(localAddress, port);
    // start Listener 
    listener.Start();
    // ..
  }
}