Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
O IPEndPoint é uma classe fundamental no desenvolvimento de aplicações de rede em .NET, especialmente quando se trabalha com sockets. Ele representa um ponto de extremidade de rede como uma combinação de um endereço IP e um número de porta. No ambiente Windows, o IPEndPoint é amplamente utilizado em aplicações que necessitam de comunicação de rede, seja para servidores ou clientes. Este artigo irá mostrar como criar e utilizar IPEndPoint no ambiente Windows, com exemplos práticos em C# e comandos úteis no PowerShell.
Exemplo 1: Criando um IPEndPoint em C#
using System;
using System.Net;
using System.Net.Sockets;
class Program
{
static void Main()
{
// Define o endereço IP e a porta
IPAddress ipAddress = IPAddress.Parse("192.168.1.1");
int port = 8080;
// Cria o IPEndPoint
IPEndPoint endPoint = new IPEndPoint(ipAddress, port);
Console.WriteLine("IPEndPoint criado: " + endPoint.ToString());
// Exemplo de uso com um socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(endPoint);
Console.WriteLine("Socket vinculado ao IPEndPoint.");
}
}
Exemplo 2: Verificando informações de rede via PowerShell
# Obtendo o endereço IP da máquina
$ipAddress = (Get-NetIPAddress -AddressFamily IPv4).IPAddress
Write-Output "Endereço IP: $ipAddress"
# Definindo a porta
$port = 8080
# Exibindo o IPEndPoint
Write-Output "IPEndPoint: $ipAddress:$port"
Exemplo 3: Utilizando IPEndPoint para conexão TCP em C#
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Program
{
static void Main()
{
IPAddress ipAddress = IPAddress.Parse("192.168.1.1");
int port = 8080;
IPEndPoint endPoint = new IPEndPoint(ipAddress, port);
// Cria e conecta o socket
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(endPoint);
Console.WriteLine("Conectado ao servidor.");
// Envia uma mensagem
string message = "Olá, servidor!";
byte[] data = Encoding.UTF8.GetBytes(message);
clientSocket.Send(data);
Console.WriteLine("Mensagem enviada.");
// Recebe uma resposta
byte[] buffer = new byte[1024];
int bytesReceived = clientSocket.Receive(buffer);
Console.WriteLine("Resposta recebida: " + Encoding.UTF8.GetString(buffer, 0, bytesReceived));
// Fecha o socket
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}