简体   繁体   English

使用TCP套接字的Delphi和C#应用程序之间没有通信

[英]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. C#-Application是TCP-Server,而Delphi-Application代表TCP-Client轮询服务器以获取信息。

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. 我正在将Microsoft Visual Studio Community 2017和.NET-Framework 4.5.1用于C#-Application,将Delphi 7(Embarcadero)用于Client-Application。

Both Applications run on the same PC, therefore I set IP-Address to "localhost" or "127.0.0.1". 两个应用程序都在同一台PC上运行,因此我将IP地址设置为“ localhost”或“ 127.0.0.1”。 The Port is 12345. 端口是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. 我的问题是,当套接字尝试连接到服务器时,我在客户端(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;

In Delphi I tried the component TcpClient as well as the Indy component TIdTCPClient - the error appears on both. 在Delphi中,我尝试了组件TcpClient和Indy组件TIdTCPClient-错误都出现在两者上。

For testing purpose I wrote a Client-Application in C# where the error didn't occur (same address and port). 为了进行测试,我用C#编写了一个Client-Application,其中没有发生错误(相同的地址和端口)。

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. 我的猜测是,C#和Delphi之间有一些联系,但我不知道是什么。

Did anyone has an idea why I cannot establish a connection with the Delphi-Application? 有谁知道为什么我无法与Delphi-Application建立连接?


Update: It seems to work, when the C#-Server accepts Clients from any IP-Address. 更新:当C#服务器从任何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);
//..

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. 在C#中,localAddress可以是地址族IPv4,IPv6或其他地址,我无法与使用IPv4的Delphi-Application连接。 My current solution: I loop through the AddressList and make sure that I get an IP-Address of IPv4-Family. 我当前的解决方案:我遍历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();
    // ..
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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