Delphi 和使用 TCP 套接字的 C# 应用程序之间没有通信
No Communication between Delphi and C# Applications using TCP-Sockets
C#-Application 是 TCP-Server,Delphi-Application 代表 TCP-Client 轮询服务器以获取信息。
我正在为 C# 应用程序使用 Microsoft Visual Studio Community 2017 和 .NET-Framework 4.5.1,为客户端应用程序使用 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 "Connection refused"。
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-应用程序建立连接?
更新:当 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 或其他地址族,我无法连接我的 Delphi-适用于 IPv4 的应用程序。
我目前的解决方案:
我遍历 AddressList 并确保获得 IPv4 系列的 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();
// ..
}
}
C#-Application 是 TCP-Server,Delphi-Application 代表 TCP-Client 轮询服务器以获取信息。
我正在为 C# 应用程序使用 Microsoft Visual Studio Community 2017 和 .NET-Framework 4.5.1,为客户端应用程序使用 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 "Connection refused"。
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-应用程序建立连接?
更新:当 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 或其他地址族,我无法连接我的 Delphi-适用于 IPv4 的应用程序。 我目前的解决方案: 我遍历 AddressList 并确保获得 IPv4 系列的 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();
// ..
}
}