2009-05-20 13 views
0

なぜこの時点でLocalEndpoint = 0.0.0.0ですか?ドキュメントによれば、システムによって選択された適切なアドレスでなければなりません。注:これは一部のマシンでのみ発生します。ほとんどのマシンは私が期待するIPアドレスを返します。これは.NETのバグですか?一部のマシンでC#Socket.LocalEndPointが0.0.0.0を返します

using (Socket s = new Socket(AddressFamily.InterNetwork, 
    SocketType.Stream, ProtocolType.Tcp)) 
    { 
     Console.WriteLine("Connecting"); 
     s.Connect("www.google.com", 80); 
     Console.WriteLine("Connected OK"); 
     s.Send(new byte[] { 1 }); 
     Console.WriteLine("Sent Byte OK"); 
     Console.WriteLine("Local EndPoint = " + 
     s.LocalEndPoint.ToString()); 
    } 
    //Local EndPoint = 0.0.0.0 

私はまた、直接ソケットを作成した後

s.Bind(new IPEndPoint(IPAddress.Any, 0)); 

をやって試してみましたが、それは何の違いが行われていません。 「問題」マシンは常に0.0.0.0を返します。

Media State . . . . . . . . . . . : Media disconnected 
    Description . . . . . . . . . . . : VIA VT6105 Rhine Fast Ethernet Adapter 
    Physical Address. . . . . . . . . : 00-30-18-67-A0-EB 

イーサネットアダプタローカルエリア接続:

Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : Realtek RTL8029 PCI Ethernet Adapter 
    Physical Address. . . . . . . . . : 00-C0-DF-E7-C9-5D 
    Dhcp Enabled. . . . . . . . . . . : Yes 
    Autoconfiguration Enabled . . . . : Yes 
    IP Address. . . . . . . . . . . . : 10.0.0.6 
    Subnet Mask . . . . . . . . . . . : 255.0.0.0 
    Default Gateway . . . . . . . . . : 10.0.0.2 
    DHCP Server . . . . . . . . . . . : 10.0.0.2 
    DNS Servers . . . . . . . . . . . : 10.0.0.2 
    Lease Obtained. . . . . . . . . . : Wednesday, May 20, 2009 5:39:06 PM 
    Lease Expires . . . . . . . . . . : Thursday, May 21, 2009 5:39:06 PM 

10.0.0.6ここ

は、ipconfig/allと

Windows IP Configuration 

    Host Name . . . . . . . . . . . . : andrepc 
    Primary Dns Suffix . . . . . . . : 
    Node Type . . . . . . . . . . . . : Unknown 
    IP Routing Enabled. . . . . . . . : No 
    WINS Proxy Enabled. . . . . . . . : No 

イーサネットアダプタローカルエリア接続2の結果であります結果として私が期待するIPだろうか。

+0

マシンのコマンドコンソールで「ipconfig/all」と入力してみてください。 – ChrisW

答えて

1

解決方法を使用することにしました。テストされ、動作します。誰かがこの方法で正確なIP /ポートを与えない理由を考えることができますか? 「0.0.0.0」は、基本的にすべてのネットワークアダプタを聞くためにソケットを伝えますので、

static void Main(string[] args) 
{ 
    try 
    { 
     using (Socket s = new Socket(AddressFamily.InterNetwork, 
      SocketType.Stream, ProtocolType.Tcp)) 
     { 
      Console.WriteLine("Connecting"); 
      s.Connect("www.google.com", 80); 
      Console.WriteLine("Connected OK"); 
      s.Send(new byte[] { 1 }); 
      Console.WriteLine("Sent Byte OK"); 
      Console.WriteLine("Local EndPoint Netstat  = " + 
       GetLocalEndPoint(s)); 
      Console.WriteLine("Local EndPoint LocalEndPoint = " + 
       ((IPEndPoint)s.LocalEndPoint).ToString()); 
     } 
    } 
    catch (Exception e) 
    { 
     Console.WriteLine("Exception - " + e.Message); 
    } 
} 

static string GetLocalEndPoint(Socket s) 
{ 
    Console.WriteLine(DateTime.Now.TimeOfDay.ToString() + 
     " Find IP LocalEndPoint"); 

    IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); 
    TcpConnectionInformation[] connections = 
     properties.GetActiveTcpConnections(); 
    IPEndPoint remoteEP = (IPEndPoint)s.RemoteEndPoint; 
    foreach (TcpConnectionInformation netstat in connections) 
    { 
     if (remoteEP.ToString() == netstat.RemoteEndPoint.ToString()) 
     { 
      Console.WriteLine(DateTime.Now.TimeOfDay.ToString() + 
       " Find IP LocalEndPoint OK"); 
      //Use the "Netstat" IP but the socket.LocalEndPoint port 
      return new IPEndPoint(netstat.LocalEndPoint.Address, 
       ((IPEndPoint)s.LocalEndPoint).Port).ToString(); 
     } 
    } 
    return string.Empty; 
} 

Connecting 
Connected OK 
Sent Byte OK 
09:57:38.5312500 Find IP LocalEndPoint 
09:57:38.5312500 Find IP LocalEndPoint OK 
Local EndPoint Netstat  = 10.0.0.6:1711 
Local EndPoint LocalEndPoint = 0.0.0.0:1711 
+0

これは、所与のリモートエンドポイントにバインドされたローカルエンドポイントが1つしかない場合には有効です。 – EricLaw

0

((IPEndPoint)s.LocalEndPoint).ToString());を使用してください。

+0

違いはありません。依然として0.0.0.0 – Kevin

+0

ファイアウォールクライアント(ISAファイアウォールクライアントなど)がインストールされていますか? マルチホームマシンはありますか? – EricLaw

+0

ファイアウォールクライアントがインストールされていません。私はISAファイアウォールクライアントを自分で疑った。 – Kevin

2

私はあなたが私がテストしたものから、あなたのソケット上のローカルエンドポイントの正しい結果を得ていると思いますが、基本的にはデフォルトのIPのthatsあなたがそのソケットで聴くならば。 IPEndpoint.Anyプロパティは基本的に "0.0.0.0"のipに等しい。

私はあなたがその方法でそれを解決しているので、netstatでローカルIPを取得すると思います。

0

Windowsのコマンドラインコマンド "ipconfig -all"がが接続されていないアダプタを返すと、IP 0.0.0.0が返されます。

「閉鎖/停止」(私は英語の単語を正確にはわかりませんが、英語のWindowsはありません)のアダプターを使用すると、正しいIPが返されることがわかります。

関連する問題