İngilizce dilinde oku

Aracılığıyla paylaş


IPEndPoint Sınıf

Tanım

Bir ağ uç noktasını IP adresi ve bağlantı noktası numarası olarak temsil eder.

C#
public class IPEndPoint : System.Net.EndPoint
C#
[System.Serializable]
public class IPEndPoint : System.Net.EndPoint
Devralma
IPEndPoint
Öznitelikler

Örnekler

C#

// This example uses the IPEndPoint class and its members to display the home page
// of the server selected by the user.

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;

namespace Mssc.Services.ConnectionManagement
{
  public class TestIPEndPoint
  {

    // The getPage method gets the server's home page content by
    // recreating the server's endpoint from the original serialized endpoint.
    // Then it creates a new socket and connects it to the endpoint.
    private static string getPage(string server, SocketAddress socketAddress)
    {
      //Set up variables and string to write to the server.
      Encoding ASCII = Encoding.ASCII;
      string Get = "GET / HTTP/1.1\r\nHost: " + server +
        "\r\nConnection: Close\r\n\r\n";
      Byte[] ByteGet = ASCII.GetBytes(Get);
      Byte[] RecvBytes = new Byte[256];
      String strRetPage = null;

      Socket socket = null;

      // Recreate the connection endpoint from the serialized information.
      IPEndPoint endpoint = new IPEndPoint(0,0);
      IPEndPoint clonedIPEndPoint = (IPEndPoint) endpoint.Create(socketAddress);
      Console.WriteLine("clonedIPEndPoint: " + clonedIPEndPoint.ToString());

      Console.WriteLine("Press any key to continue.");
      Console.ReadLine();

      try
      {
        // Create a socket object to establish a connection with the server.
        socket =
          new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

        // Connect to the cloned end point.
        socket.Connect(clonedIPEndPoint);
      }
      catch(SocketException e)
      {
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
      }
      catch(Exception e)
      {
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
      }

      if (socket == null)
        return ("Connection to cloned endpoint failed");

      // Send request to the server.
      socket.Send(ByteGet, ByteGet.Length, 0);

      // Receive the server  home page content.
      Int32 bytes = socket.Receive(RecvBytes, RecvBytes.Length, 0);

      // Read the first 256 bytes.
      strRetPage = "Default HTML page on " + server + ":\r\n";
      strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);

      while (bytes > 0)
      {
        bytes = socket.Receive(RecvBytes, RecvBytes.Length, 0);
        strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
      }

      socket.Close();

      return strRetPage;
    }

    // The serializeEndpoint method serializes the endpoint and returns the
    // SocketAddress containing the serialized endpoint data.
    private static SocketAddress serializeEndpoint(IPEndPoint endpoint)
    {

      // Serialize IPEndPoint details to a SocketAddress instance.
      SocketAddress socketAddress = endpoint.Serialize();

      // Display the serialized endpoint information.
      Console.WriteLine("Endpoint.Serialize() : " + socketAddress.ToString());

      Console.WriteLine("Socket.Family : " + socketAddress.Family);
      Console.WriteLine("Socket.Size : " + socketAddress.Size);

      Console.WriteLine("Press any key to continue.");
      Console.ReadLine();

      return socketAddress;
    }

    private static void displayEndpointInfo(IPEndPoint endpoint)
    {
      Console.WriteLine("Endpoint.Address : " + endpoint.Address);
      Console.WriteLine("Endpoint.AddressFamily : " + endpoint.AddressFamily);
      Console.WriteLine("Endpoint.Port : " + endpoint.Port);
      Console.WriteLine("Endpoint.ToString() : " + endpoint.ToString());

      Console.WriteLine("Press any key to continue.");
      Console.ReadLine();
    }

    // The serializeEndpoint method determines the server endpoint and then
    // serializes it to obtain the related SocketAddress object.
    // Note that in the for loop a temporary socket is created to ensure that
    // the current IP address format matches the AddressFamily type.
    // In fact, in the case of servers supporting both IPv4 and IPv6, an exception
    // may arise if an IP address format does not match the address family type.
    private static SocketAddress getSocketAddress(string server, int port)
    {
      Socket tempSocket = null;
      IPHostEntry host = null;
      SocketAddress serializedSocketAddress = null;

      try
      {
        // Get the object containing Internet host information.
        host = Dns.Resolve(server);

        // Obtain the IP address from the list of IP addresses associated with the server.
        foreach(IPAddress address in host.AddressList)
        {
          IPEndPoint endpoint = new IPEndPoint(address, port);

          tempSocket =
            new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

          tempSocket.Connect(endpoint);

          if(tempSocket.Connected)
          {
            // Display the endpoint information.
            displayEndpointInfo(endpoint);
            // Serialize the endpoint to obtain a SocketAddress object.
            serializedSocketAddress = serializeEndpoint(endpoint);
            break;
          }
          else
                    {
                        continue;
                    }
                }

        // Close the temporary socket.
        tempSocket.Close();
      }

      catch(SocketException e)
      {
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
      }
      catch(Exception e)
      {
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
      }
      return serializedSocketAddress;
    }

    // The requestServerHomePage method obtains the server's home page and returns
    // its content.
    private static string requestServerHomePage(string server, int port)
    {
      String strRetPage = null;

      // Get a socket address using the specified server and port.
      SocketAddress socketAddress = getSocketAddress(server, port);

      if (socketAddress == null)
        strRetPage = "Connection failed";
      else
        // Obtain the server's home page content.
        strRetPage = getPage(server, socketAddress);

      return strRetPage;
    }

    // Show to the user how to use this program when wrong input parameters are entered.
    private static void showUsage()
    {
      Console.WriteLine("Enter the server name as follows:");
      Console.WriteLine("\tcs_ipendpoint servername");
    }

    // This is the program entry point. It allows the user to enter
    // a server name that is used to locate its current homepage.
    public static void Main(string[] args)
    {
      string host= null;
      int port = 80;

      // Define a regular expression to parse user's input.
      // This is a security check. It allows only
      // alphanumeric input string between 2 to 40 character long.
      Regex rex = new Regex(@"^[a-zA-Z]\w{1,39}$");

      if (args.Length < 1)
            {
                showUsage();
            }
            else
      {
        string message = args[0];
        if ((rex.Match(message)).Success)
        {
          host = args[0];
          // Get the specified server home_page and display its content.
          string result = requestServerHomePage(host, port);
          Console.WriteLine(result);
        }
        else
                {
                    Console.WriteLine("Input string format not allowed.");
                }
            }
    }
  }
}

Açıklamalar

sınıfı, IPEndPoint bir uygulamanın konak üzerindeki bir hizmete bağlanmak için ihtiyaç duyduğu konak ve yerel veya uzak bağlantı noktası bilgilerini içerir. Sınıfı, IPEndPoint konağın IP adresini ve bir hizmetin bağlantı noktası numarasını birleştirerek bir hizmete bağlantı noktası oluşturur.

Oluşturucular

IPEndPoint(Int64, Int32)

Belirtilen adres ve bağlantı noktası numarasıyla sınıfının yeni bir örneğini IPEndPoint başlatır.

IPEndPoint(IPAddress, Int32)

Belirtilen adres ve bağlantı noktası numarasıyla sınıfının yeni bir örneğini IPEndPoint başlatır.

Alanlar

MaxPort

Özelliğine atanabilecek Port en büyük değeri belirtir. MaxPort değeri 0x0000FFFF olarak ayarlanır. Bu alan salt okunur durumdadır.

MinPort

Özelliğine atanabilecek Port en düşük değeri belirtir. Bu alan salt okunur durumdadır.

Özellikler

Address

Uç noktanın IP adresini alır veya ayarlar.

AddressFamily

İnternet Protokolü (IP) adres ailesini alır.

Port

Uç noktanın bağlantı noktası numarasını alır veya ayarlar.

Yöntemler

Create(SocketAddress)

Yuva adresinden bir uç nokta oluşturur.

Equals(Object)

Belirtilen Object öğesinin geçerli Objectöğesine eşit olup olmadığını belirler.

GetHashCode()

Örnek için bir IPEndPoint karma değeri döndürür.

GetType()

Type Geçerli örneğini alır.

(Devralındığı yer: Object)
MemberwiseClone()

Geçerli Objectöğesinin sığ bir kopyasını oluşturur.

(Devralındığı yer: Object)
Parse(ReadOnlySpan<Char>)

Salt okunur bir yayılma alanı olarak temsil edilen IP ağ uç noktasını (adres ve bağlantı noktası) örneğe IPEndPoint dönüştürür.

Parse(String)

Dize olarak temsil edilen ip ağ uç noktasını (adres ve bağlantı noktası) örneğe IPEndPoint dönüştürür.

Serialize()

Uç nokta bilgilerini bir SocketAddress örneğe serileştirir.

ToString()

Belirtilen uç noktanın IP adresini ve bağlantı noktası numarasını döndürür.

TryParse(ReadOnlySpan<Char>, IPEndPoint)

Salt okunur bir span olarak temsil edilen bir IP ağ uç noktasını (adres ve bağlantı noktası) eşdeğerine IPEndPoint dönüştürmeye çalışır ve dönüştürmenin başarılı olup olmadığını belirten bir değer döndürür.

TryParse(String, IPEndPoint)

Dize olarak temsil edilen bir IP ağ uç noktasını (adres ve bağlantı noktası) eşdeğerine IPEndPoint dönüştürmeye çalışır ve dönüştürmenin başarılı olup olmadığını belirten bir değer döndürür.

Şunlara uygulanır

Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0