2009-05-20 3 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 여기

모든

Windows IP Configuration 

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

이더넷 어댑터 로컬 영역 연결 2 /에서 ipconfig의 결과입니다 그 결과로 내가 기대할 수있는 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를 먹으 렴 그 소켓으로 들으면. IPEndpoint.Any 속성은 기본적으로 "0.0.0.0"의 ip와 같습니다.

나는 당신이 그 방법으로 그것을 해결하고 있기 때문에 netstat로 당신의 지방의 ip를 얻는다고 생각한다.

0

우리는 지금 그것을 테스트했으며 "ipconfig -all"windows 명령 줄 명령이 연결이 끊어진 어댑터를 반환 할 때 IP 0.0.0.0이 반환됩니다.

"폐쇄/중지"(영어로 된 단어가 정확히 무엇인지 모르는 경우 영어로 된 Windows가 없습니다) 어댑터를 사용하면 올바른 IP가 반환되는 것을 볼 수 있습니다.

관련 문제