简体   繁体   中英

No Communication between Delphi and C# Applications using TCP-Sockets

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);
//..

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();
    // ..
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM