繁体   English   中英

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

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

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

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

两个应用程序都在同一台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#编写了一个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();
  }
}

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

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


更新:当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);
//..

在C#中,localAddress可以是地址族IPv4,IPv6或其他地址,我无法与使用IPv4的Delphi-Application连接。 我当前的解决方案:我遍历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