Re: UDP broadcasting problem



-------------My OnConnect and OnReceive Events Mthods-----------------

private void Client_OnConnect( string msg )
{
FileLogger Log = FileLogger.New;
Log.AppendToLogFile("Connected.");
Console.WriteLine( "Connected.");
}
private void Client_OnRecieve( string msg )
{

FileLogger Log = FileLogger.New;
Log.AppendToLogFile( msg );
Console.WriteLine(msg);
}



--------------My Multicasting code:-------------------------------

namespace DigiSoft.Multicasting
{
/// <summary>
/// Summary description for Multicasting.
/// </summary>
///
public delegate void OnRecieveDataEventHandler( string Message );
public delegate void OnConnectEventHandler(string Message );
public delegate void OnDisconnectEventHandler( string Message );
public delegate void OnErrorEventHandler( string errorMsg );

public class Multicasting
{
//Properties
public string MulticastGroup
{
get { return mygroupAddress; }
set { mygroupAddress = value; }
}

public int groupPort
{
get { return mygroupPort; }
set { mygroupPort = value; }
}

public int TTL
{
get { return myTTL; }
set { myTTL = value; }
}


private UdpClient thisClient;
private IPEndPoint RemoteEndPoint;
private string mygroupAddress;
private int mygroupPort;
private int myTTL;
private bool done;
private bool isRunning = false;

public void Start()
{
try
{
UdpClient udp = AsyncState as UdpClient;
thisClient= new UdpClient( mygroupPort );
thisClient.JoinMulticastGroup( IPAddress.Parse(mygroupAddress), myTTL );
RemoteEndPoint = new IPEndPoint(IPAddress.Parse(mygroupAddress),
mygroupPort);

Thread myThread = new Thread( new ThreadStart( this.Listen ));
#if !CF
myThread.IsBackground = true;
#endif
myThread.Start();
isRunning = true;


if ( OnConnect != null)
OnConnect(string.Empty );

//send whereabouts to the group
byte[] bs = System.Text.Encoding.UTF8.GetBytes( Dns.GetHostByName(
Dns.GetHostName()).AddressList[0].ToString() + " has joined." ) ;
thisClient.Send(bs,(int) bs.Length, RemoteEndPoint);

}
catch(Exception e)
{
//Some error Message;
if(OnError!=null)
OnError("Error Connecting : " + e.Message);
done = true;
isRunning = false;
}
}

private void Listen()
{
done = false;
while(!done)
{

try
{
#if !CF
IPEndPoint IPEndPointNull = null;
#else

IPEndPoint IPEndPointNull = new IPEndPoint( IPAddress.Any,
this.mygroupPort );
#endif
byte[] bs = thisClient.Receive( ref IPEndPointNull);
string msg = System.Text.Encoding.UTF8.GetString(bs,0,bs.Length);

if ( OnRecieve != null )
OnRecieve(msg);
}
catch(Exception e)
{
//Some Error Message
if(OnError!=null)
OnError("Error Recieving: " + e.Message);
done = true;
isRunning = false;

}
}
}

public void Stop()
{

if ( !isRunning ) return ;

//Say your goodbyes to all the members of the group...
done = true;
isRunning = false;

byte[] bs = System.Text.Encoding.UTF8.GetBytes( Dns.GetHostByName(
Dns.GetHostName() ).AddressList[0].ToString() + " has left." ) ;
thisClient.Send(bs,(int) bs.Length, RemoteEndPoint);

#if CF
Thread.Sleep(300);
#endif

if( OnDisconnect!=null)
OnDisconnect( string.Empty );


try
{
thisClient.DropMulticastGroup(IPAddress.Parse(mygroupAddress));
thisClient.Close();
}
catch(Exception e)
{
if(OnError!=null)
OnError("Error Closing: " + e.Message);
}
}

public void Send( string Message)
{
if ( !isRunning ) return ;

try
{
byte[] bs = System.Text.Encoding.UTF8.GetBytes(Message);
thisClient.Send(bs,(int) bs.Length, RemoteEndPoint);
}
catch(Exception e)
{
//Some Error Message
if(OnError!=null)
OnError("Error Sending: " + e.Message);
done = true;
isRunning = false;
}
}


public event OnRecieveDataEventHandler OnRecieve;
public event OnConnectEventHandler OnConnect;
public event OnDisconnectEventHandler OnDisconnect;
public event OnErrorEventHandler OnError;
}
}
.



Relevant Pages

  • Re: UDP broadcasting problem
    ... private void Client_OnConnect(string msg) ... /// public delegate void OnRecieveDataEventHandler(string Message); ... get {return mygroupPort;} ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: The fastest way to determine c = (byte)a & b
    ... private void InitializeComponent() ... string getHexString() { ... private void currentTextBoxChangedEventHander(object sender, EventArgs e) ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Binding custom object to formview
    ... private void SetPersonInaccessibleDetails ... protected void ds_Inserting(object sender, ObjectDataSourceMethodEventArgs ... private FullName fullName = new FullName; ... private string firstName; ...
    (microsoft.public.dotnet.framework.aspnet)
  • Applet to JFrame GUI
    ... I am working on making an applet into a JFrame GUI and getting three ... public static void main ... private JLabel calcNameJLabel; ... String balString = balFieldJTextField.getText; ...
    (comp.lang.java.gui)
  • Swicthing from an Applet to a JFrame
    ... I am working on making an applet into a JFrame GUI and getting three ... public static void main ... private JLabel calcNameJLabel; ... String balString = balFieldJTextField.getText; ...
    (comp.lang.java.help)