Получение IP-адреса удаленной конечной точки сокета

Я думаю, что это - полностью вопрос стиля. Ни у одного действительно нет очевидного преимущества перед другим.

Непротиворечивость более важна, чем любой конкретный выбор, таким образом, я рекомендовал бы, чтобы Вы собрались со своей командой и выбрали один стиль и придерживались его.

36
задан bobber205 15 December 2009 в 00:02
поделиться

2 ответа

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.remoteendpoint.aspx

You can then call the IPEndPoint..::.Address method to retrieve the remote IPAddress, and the IPEndPoint..::.Port method to retrieve the remote port number.

More from the link (fixed up alot heh):

Socket s;

IPEndPoint remoteIpEndPoint = s.RemoteEndPoint as IPEndPoint;
IPEndPoint localIpEndPoint = s.LocalEndPoint as IPEndPoint;

if (remoteIpEndPoint != null)
{
    // Using the RemoteEndPoint property.
    Console.WriteLine("I am connected to " + remoteIpEndPoint.Address + "on port number " + remoteIpEndPoint.Port);
}

if (localIpEndPoint != null)
{
    // Using the LocalEndPoint property.
    Console.WriteLine("My local IpAddress is :" + localIpEndPoint.Address + "I am connected on port number " + localIpEndPoint.Port);
}
63
ответ дан 27 November 2019 в 05:47
поделиться

RemoteEndPoint - это свойство, его тип - System.Net.EndPoint , наследуемый от System.Net.IPEndPoint .

Если взять взглянув на членов IPEndPoint , вы увидите, что есть свойство Address .

6
ответ дан 27 November 2019 в 05:47
поделиться
Другие вопросы по тегам:

Похожие вопросы: