2017-07-31 22 views
0

C#アプリケーションはTCPサーバーであり、Delphiアプリケーションは情報のためのTCPクライアントポーリングサーバーを表します。TCPソケットを使用したDelphiとC#アプリケーション間の通信なし

私は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); 
    }  
    } 
} 

である私の問題は、私はソケットがサーバーに接続しようとすると、エラー#10061のクライアントで(デルファイ)アプリケーションを「接続が拒否」を得ること、です。 Delphiで

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; 

Iは成分れるtcpClientならびにインディコンポーネント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アプリケーションとの接続を確立できないのでしょうか?


アップデート: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); 
//.. 
+2

C#サーバーのコードを投稿することはできますか? –

+0

上記の質問にC#serverとC#clientのコードを追加しました。 – topet

+0

サーバーサイドがどのようにリスニングしているかを確認するコードがもう少し必要です(MSDN docsサンプルのhttps://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v = vs.110).aspx)。 –

答えて

0

C#localAddressには、アドレスファミリのIPv4、IPv6などがありますが、IPv4で動作するDelphiアプリケーションに接続できませんでした。 私の現在の解決策: 私は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(); 
    // .. 
    } 
} 
関連する問題