//#
//# Multicast Class based on Java's java.net.MulticastSocket
//# $Revision: 1.1 $
//# Copyright 2004 by Eric Y. Theriault
//# All Rights Reserved.
//# http://www.eyt.ca/CORBA
//#
using System;
using System.Net;
using System.Net.Sockets;
namespace ca.eyt.corba
{
//
// Ideally, this class should be using UdpClient, however, the class
// does not support timeouts and have problems when multiple servers
// respond.
//
///
/// Implements a Multicast Socket Wrapper
///
///
/// Based on java.net.MulticastSocket, implements some required functions
/// for this CORBA project. More information regarding this can be
/// found in Unix Network Programming: Volume 1.
///
public class MulticastSocket
{
///
/// Port to bind
///
private int port;
///
/// Timeout
///
private int timeout;
///
/// Socket
///
private Socket socket;
///
/// Constructs a socket on the specified port.
///
///
/// Port is the UDP port to multicast on.
/// Bind denotes whether to bind or not; only the server should
/// bind.
///
public MulticastSocket( int port )
{
this.port = port;
this.timeout = 0;
socket = new System.Net.Sockets.Socket(
AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp
);
}
///
/// Destructor
///
public void close()
{
if ( socket != null ) {
socket.Close();
socket = null;
}
}
///
/// Bind the port accordingly.
///
///
/// This method is provided for servers.
///
public void bind()
{
socket.Bind( new IPEndPoint( IPAddress.Any, port ) );
}
///
/// Joins a Multicast Group.
///
///
/// The address is a multicast group address, such as
/// 224.0.0.1.
/// The ifaceAddress is the address of the interface to multicast
/// on to; a null pointer will use the default route.
///
/// Address to join
public void joinGroup( String address )
{
IPAddress ip = IPAddress.Parse( address );
socket.SetSocketOption(
SocketOptionLevel.IP,
SocketOptionName.AddMembership,
new MulticastOption( ip, IPAddress.Any )
);
}
///
/// Send a packet.
///
/// Address to send to, either multicast or unicast
/// Some string to send
public void send(
String address,
String packet
)
{
IPAddress ip = IPAddress.Parse( address );
IPEndPoint remote = new IPEndPoint( ip, port );
send( remote, packet );
}
///
/// Send a packet.
///
/// Address to send to, either multicast or unicast
/// Some string to send
public void send(
IPEndPoint address,
String packet
)
{
socket.SendTo(
System.Text.Encoding.ASCII.GetBytes( packet ),
address
);
}
///
/// Set the timeout
///
/// Timeout in milliseconds. 0 disables.
/// Negative is undefined.
public void setSoTimeout( int timeout )
{
this.timeout = timeout;
}
///
/// Receives a packet.
///
/// Addressed received from.
/// String received.
public string receive( ref IPEndPoint address )
{
if ( timeout != 0 ) {
// Wait for input...
if ( socket.Poll( timeout, SelectMode.SelectRead ) == false ) {
throw new Exception( "Timeout." );
}
}
// Process read
byte[] buffer = new byte[1000];
address = new System.Net.IPEndPoint( System.Net.IPAddress.Any, port );
EndPoint senderRemote = (address);
int i = socket.ReceiveFrom( buffer, 0, buffer.Length, SocketFlags.None, ref senderRemote );
address = (IPEndPoint)senderRemote;
return System.Text.Encoding.ASCII.GetString( buffer, 0, i );
}
}
}