MulticastOption 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
IPAddress 멀티캐스트 그룹을 조인 및 삭제하는 데 사용되는 값을 포함합니다.
public ref class MulticastOption
public class MulticastOption
type MulticastOption = class
Public Class MulticastOption
- 상속
-
MulticastOption
예제
다음 예제에서는 기본 IP 인터페이스를 IP 멀티캐스트 그룹에 조인합니다. 224.0.0.0에서 239.255.255.255 범위의 IP 멀티캐스트 그룹 주소를 가정합니다.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
// This is the listener example that shows how to use the MulticastOption class.
// In particular, it shows how to use the MulticastOption(IPAddress, IPAddress)
// constructor, which you need to use if you have a host with more than one
// network card.
// The first parameter specifies the multicast group address, and the second
// specifies the local address of the network card you want to use for the data
// exchange.
// You must run this program in conjunction with the sender program as
// follows:
// Open a console window and run the listener from the command line.
// In another console window run the sender. In both cases you must specify
// the local IPAddress to use. To obtain this address run the ipconfig command
// from the command line.
//
namespace Mssc.TransportProtocols.Utilities
{
public class TestMulticastOption
{
private static IPAddress s_mcastAddress;
private static int s_mcastPort;
private static Socket s_mcastSocket;
private static MulticastOption s_mcastOption;
private static void MulticastOptionProperties()
{
Console.WriteLine("Current multicast group is: " + s_mcastOption.Group);
Console.WriteLine("Current multicast local address is: " + s_mcastOption.LocalAddress);
}
private static void StartMulticast()
{
try
{
s_mcastSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
Console.Write("Enter the local IP address: ");
IPAddress localIPAddr = IPAddress.Parse(Console.ReadLine());
//IPAddress localIP = IPAddress.Any;
EndPoint localEP = (EndPoint)new IPEndPoint(localIPAddr, s_mcastPort);
s_mcastSocket.Bind(localEP);
// Define a MulticastOption object specifying the multicast group
// address and the local IPAddress.
// The multicast group address is the same as the address used by the server.
s_mcastOption = new MulticastOption(s_mcastAddress, localIPAddr);
s_mcastSocket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
s_mcastOption);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ReceiveBroadcastMessages()
{
bool done = false;
byte[] bytes = new byte[100];
IPEndPoint groupEP = new(s_mcastAddress, s_mcastPort);
EndPoint remoteEP = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
try
{
while (!done)
{
Console.WriteLine("Waiting for multicast packets.......");
Console.WriteLine("Enter ^C to terminate.");
s_mcastSocket.ReceiveFrom(bytes, ref remoteEP);
Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes, 0, bytes.Length));
}
s_mcastSocket.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void Main(string[] args)
{
// Initialize the multicast address group and multicast port.
// Both address and port are selected from the allowed sets as
// defined in the related RFC documents. These are the same
// as the values used by the sender.
s_mcastAddress = IPAddress.Parse("224.168.100.2");
s_mcastPort = 11000;
// Start a multicast group.
StartMulticast();
// Display MulticastOption properties.
MulticastOptionProperties();
// Receive broadcast messages.
ReceiveBroadcastMessages();
}
}
}
' This is the listener example that shows how to use the MulticastOption class.
' In particular, it shows how to use the MulticastOption(IPAddress, IPAddress)
' constructor, which you need to use if you have a host with more than one
' network card.
' The first parameter specifies the multicast group address, and the second
' specifies the local address of the network card you want to use for the data
' exchange.
' You must run this program in conjunction with the sender program as
' follows:
' Open a console window and run the listener from the command line.
' In another console window run the sender. In both cases you must specify
' the local IPAddress to use. To obtain this address run the ipconfig command
' from the command line.
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Namespace Mssc.TransportProtocols.Utilities
Module M_TestMulticastOption
Public Class TestMulticastOption
Private Shared mcastAddress As IPAddress
Private Shared mcastPort As Integer
Private Shared mcastSocket As Socket
Private Shared mcastOption As MulticastOption
Private Shared Sub MulticastOptionProperties()
Console.WriteLine(("Current multicast group is: " + mcastOption.Group.ToString()))
Console.WriteLine(("Current multicast local address is: " + mcastOption.LocalAddress.ToString()))
End Sub
Private Shared Sub StartMulticast()
Try
mcastSocket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Console.Write("Enter the local IP address: ")
Dim localIPAddr As IPAddress = IPAddress.Parse(Console.ReadLine())
'IPAddress localIP = IPAddress.Any;
Dim localEP As EndPoint = CType(New IPEndPoint(localIPAddr, mcastPort), EndPoint)
mcastSocket.Bind(localEP)
' Define a MulticastOption object specifying the multicast group
' address and the local IPAddress.
' The multicast group address is the same as the address used by the server.
mcastOption = New MulticastOption(mcastAddress, localIPAddr)
mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption)
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub
Private Shared Sub ReceiveBroadcastMessages()
Dim done As Boolean = False
Dim bytes() As Byte = New [Byte](99) {}
Dim groupEP As New IPEndPoint(mcastAddress, mcastPort)
Dim remoteEP As EndPoint = CType(New IPEndPoint(IPAddress.Any, 0), EndPoint)
Try
While Not done
Console.WriteLine("Waiting for multicast packets.......")
Console.WriteLine("Enter ^C to terminate.")
mcastSocket.ReceiveFrom(bytes, remoteEP)
Console.WriteLine("Received broadcast from {0} :" + ControlChars.Lf + " {1}" + ControlChars.Lf, groupEP.ToString(), Encoding.ASCII.GetString(bytes, 0, bytes.Length))
End While
mcastSocket.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub
Public Shared Sub Main(ByVal args() As String)
' Initialize the multicast address group and multicast port.
' Both address and port are selected from the allowed sets as
' defined in the related RFC documents. These are the same
' as the values used by the sender.
mcastAddress = IPAddress.Parse("224.168.100.2")
mcastPort = 11000
' Start a multicast group.
StartMulticast()
' Display MulticastOption properties.
MulticastOptionProperties()
' Receive broadcast messages.
ReceiveBroadcastMessages()
End Sub
End Class
End Module
End Namespace
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;
// This sender example must be used in conjunction with the listener program.
// You must run this program as follows:
// Open a console window and run the listener from the command line.
// In another console window run the sender. In both cases you must specify
// the local IPAddress to use. To obtain this address, run the ipconfig command
// from the command line.
//
namespace Mssc.TransportProtocols.Utilities
{
class TestMulticastOption2
{
static IPAddress s_mcastAddress;
static int s_mcastPort;
static Socket s_mcastSocket;
static void JoinMulticastGroup()
{
try
{
// Create a multicast socket.
s_mcastSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
// Get the local IP address used by the listener and the sender to
// exchange multicast messages.
Console.Write("\nEnter local IPAddress for sending multicast packets: ");
IPAddress localIPAddr = IPAddress.Parse(Console.ReadLine());
// Create an IPEndPoint object.
IPEndPoint IPlocal = new IPEndPoint(localIPAddr, 0);
// Bind this endpoint to the multicast socket.
s_mcastSocket.Bind(IPlocal);
// Define a MulticastOption object specifying the multicast group
// address and the local IP address.
// The multicast group address is the same as the address used by the listener.
MulticastOption mcastOption;
mcastOption = new MulticastOption(s_mcastAddress, localIPAddr);
s_mcastSocket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
mcastOption);
}
catch (Exception e)
{
Console.WriteLine("\n" + e.ToString());
}
}
static void BroadcastMessage(string message)
{
IPEndPoint endPoint;
try
{
//Send multicast packets to the listener.
endPoint = new IPEndPoint(s_mcastAddress, s_mcastPort);
s_mcastSocket.SendTo(ASCIIEncoding.ASCII.GetBytes(message), endPoint);
Console.WriteLine("Multicast data sent.....");
}
catch (Exception e)
{
Console.WriteLine("\n" + e.ToString());
}
s_mcastSocket.Close();
}
static void Main(string[] args)
{
// Initialize the multicast address group and multicast port.
// Both address and port are selected from the allowed sets as
// defined in the related RFC documents. These are the same
// as the values used by the sender.
s_mcastAddress = IPAddress.Parse("224.168.100.2");
s_mcastPort = 11000;
// Join the listener multicast group.
JoinMulticastGroup();
// Broadcast the message to the listener.
BroadcastMessage("Hello multicast listener.");
}
}
}
' This sender example must be used in conjunction with the listener program.
' You must run this program as follows:
' Open a console window and run the listener from the command line.
' In another console window run the sender. In both cases you must specify
' the local IPAddress to use. To obtain this address, run the ipconfig command
' from the command line.
'
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Namespace Mssc.TransportProtocols.Utilities
Module M_TestMulticastOption
Class TestMulticastOption
Private Shared mcastAddress As IPAddress
Private Shared mcastPort As Integer
Private Shared mcastSocket As Socket
Shared Sub JoinMulticastGroup()
Try
' Create a multicast socket.
mcastSocket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
' Get the local IP address used by the listener and the sender to
' exchange multicast messages.
Console.Write(ControlChars.Lf + "Enter local IPAddress for sending multicast packets: ")
Dim localIPAddr As IPAddress = IPAddress.Parse(Console.ReadLine())
' Create an IPEndPoint object.
Dim IPlocal As New IPEndPoint(localIPAddr, 0)
' Bind this endpoint to the multicast socket.
mcastSocket.Bind(IPlocal)
' Define a MulticastOption object specifying the multicast group
' address and the local IP address.
' The multicast group address is the same as the address used by the listener.
Dim mcastOption As MulticastOption
mcastOption = New MulticastOption(mcastAddress, localIPAddr)
mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption)
Catch e As Exception
Console.WriteLine((ControlChars.Lf + e.ToString()))
End Try
End Sub
Shared Sub BroadcastMessage(ByVal message As String)
Dim endPoint As IPEndPoint
Try
'Send multicast packets to the listener.
endPoint = New IPEndPoint(mcastAddress, mcastPort)
mcastSocket.SendTo(ASCIIEncoding.ASCII.GetBytes(message), endPoint)
Console.WriteLine("Multicast data sent.....")
Catch e As Exception
Console.WriteLine((ControlChars.Lf + e.ToString()))
End Try
mcastSocket.Close()
End Sub
Public Shared Sub Main(ByVal args() As String)
' Initialize the multicast address group and multicast port.
' Both address and port are selected from the allowed sets as
' defined in the related RFC documents. These are the same as the
' values used by the sender.
mcastAddress = IPAddress.Parse("224.168.100.2")
mcastPort = 11000
' Join the listener multicast group.
JoinMulticastGroup()
' Broadcast the message to the listener.
BroadcastMessage("Hello multicast listener.")
End Sub
End Class
End Module
End Namespace
설명
조인 MulticastOption 하거나 삭제하려는 멀티캐스트 그룹의 저장 IPAddress 에 사용합니다. Socket.SetSocketOption 다음 매개 변수와 함께 메서드를 사용하여 멀티캐스트 그룹에 조인합니다.
| 매개 변수 | 가치 |
|---|---|
| socketOptionLevel | SocketOptionLevel.Udp |
| socketOptionName | AddMembership |
| object | MulticastOption |
멀티캐스트 그룹을 삭제하는 데 사용합니다 DropMembership .
생성자
| Name | Description |
|---|---|
| MulticastOption(IPAddress, Int32) |
지정된 IP 멀티캐스트 그룹 주소 및 인터페이스 인덱스를 사용하여 클래스의 MulticastOption 새 인스턴스를 초기화합니다. |
| MulticastOption(IPAddress, IPAddress) |
지정된 IP 멀티캐스트 그룹 주소 및 네트워크 인터페이스와 연결된 로컬 IP 주소를 사용하여 클래스의 MulticastOption 새 인스턴스를 초기화합니다. |
| MulticastOption(IPAddress) |
지정된 IP 멀티캐스트 그룹에 대한 클래스의 MulticastOption 새 버전을 초기화합니다. |
속성
| Name | Description |
|---|---|
| Group |
멀티캐스트 그룹의 IP 주소를 가져오거나 설정합니다. |
| InterfaceIndex |
멀티캐스트 패킷을 보내고 받는 데 사용되는 인터페이스의 인덱스를 가져오거나 설정합니다. |
| LocalAddress |
멀티캐스트 그룹과 연결된 로컬 주소를 가져오거나 설정합니다. |
메서드
| Name | Description |
|---|---|
| Equals(Object) |
지정된 개체가 현재 개체와 같은지 여부를 확인합니다. (다음에서 상속됨 Object) |
| GetHashCode() |
기본 해시 함수로 사용됩니다. (다음에서 상속됨 Object) |
| GetType() |
현재 인스턴스의 Type 가져옵니다. (다음에서 상속됨 Object) |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |