【问题标题】:No Communication between Delphi and C# Applications using TCP-SocketsDelphi 和 C# 应用程序之间没有使用 TCP-Sockets 的通信
【发布时间】:2017-07-31 18:58:38
【问题描述】:

C#-Application 是 TCP-Server,Delphi-Application 代表 TCP-Client 轮询服务器以获取信息。

我将 Microsoft Visual Studio Community 2017 和 .NET-Framework 4.5.1 用于 C#-应用程序,将 Delphi 7 (Embarcadero) 用于客户端应用程序。

两个应用程序都在同一台 PC 上运行,因此我将 IP 地址设置为“localhost”或“127.0.0.1”。 端口是 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);
    }     
  }
}

我的问题是,当套接字尝试连接到服务器时,我在客户端 (Delphi) 应用程序中收到错误 #10061“连接被拒绝”。

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;

在 Delphi 中,我尝试了组件 TcpClient 以及 Indy 组件 TIdTCPClient - 两者都出现错误。

出于测试目的,我用 C# 编写了一个没有发生错误的客户端应用程序(相同的地址和端口)。

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

我的猜测是,C# 和 Delphi 之间有一些东西,但我不知道是什么。

有人知道为什么我无法与 Delphi-Application 建立连接吗?


更新:当 C#-Server 接受来自任何 IP 地址的客户端时,它似乎可以工作。

// ..
// host = Dns.GetHostEntry("localhost");
// localAddress = host.AddressList[0];
Console.WriteLine("Server waits for a client");
// init Listener
TcpListener listener = new TcpListener(IPAddress.Any, port);
//..

【问题讨论】:

  • 你能贴出C#服务器的代码吗?
  • 我在上面的问题中添加了 C# 服务器和 C# 客户端的代码。
  • 我希望有更多代码来查看服务器端是如何监听的(如 msdn.microsoft.com/en-us/library/… 上的 MSDN 文档示例)。
  • 发布的代码(绑定到 IPAddress.Any)是否有效?绑定到 localhost 的 DNS 条目中的第一个地址的注释掉的部分很可能是 IPV6 地址而不是 IPV4,它在我的 Windows 8、8.1 和 10 机器上。 Delphi 套接字(至少在版本 6 中)不支持 IPV6。 .NET 可以。
  • 是的,带有 IPAddress.Any 的代码有效。

标签: c# sockets tcp delphi-7


【解决方案1】:

在 C# 中,localAddress 可以是 IPv4、IPv6 或其他地址系列,我无法连接适用于 IPv4 的 Delphi 应用程序。 我目前的解决方案: 我遍历 AddressList 并确保获得 IPv4-Family 的 IP 地址。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    相关资源
    最近更新 更多