UDP broadcasting problem

G

Guest

Hi,

I have this problem in my UDP broadcasting, im a newbie in UDP,
any help at all would be very much appreciated. I have a working UDP
broadcast that sends and recieves without any problem. I tried sending a
message 30 times in a loop simultaneously and i recieved all of them without
any problem. But last night i tried sending 64kb message in the same loop for
30 times and all i recieved was 5 messages out of 30, im not sure if i missed
the other 25 messages or most of the messages sent were not finished.

So i tried, placing a thread.sleep(500) every before it sends a
message and everything worked out fine again, i recieved all the messages
without any problem. The problem now is that i cannot risk having a
thread.sleep(500) around as it would affect the whole system. Is there
anything i can do to recieve them completely without having to use sleep?

Can anyone suggest on something? Sample code would really really help
but anything you can suggest is good enough, i just desperately need this
one... Thank you so much in advance!!!
 
C

Chris Mullins

Rain said:
But last night i tried sending 64kb message in the same loop for
30 times and all i recieved was 5 messages out of 30, im not sure
if i missed the other 25 messages or most of the messages sent
were not finished.

I would wager that the other 25 udp packets came in, but your code missed
them. In general, it's very rare to "lose" a UDP packets - especially if
you're on a LAN, or using the loopback address on your computerer.


Depening on the scalability of what you're doing, you're going to want to
either:
1 - (low scalability requirements, easier coding) create a UDP Listen thread
for each socket you're listening on and receive data that way.
2 - (high scalability, often confusing to newcomers) Use the Begin/End
methods on the sockets and do asynchronous programming. This scales very
well, is incredibly reliable, and (once you get the hang of it) is pretty
easy. The MS primers can be found at:
http://msdn2.microsoft.com/en-us/library/ms228969.aspx
 
G

Guest

Hi Chris,

Im already doing the number one, low scalability, on the UDP im
doing that doesnt receive or process the entire messages. Since im still a
newbie here i cant really get the idea on the number 2.. Would you care to
explain? If its okay with you that is.. Begin/End are methods of Socket
right? Im not using any socket, both my receiving and seding is using
UDPClient. Thank You so much!
 
G

Guest

-------------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;
}
}
 
L

Leon Lambert

UDP is not guaranteed to be delivered. Your program has to be there and
ready to receive when the next packet comes. In your code you are
writing the message to a logger and the console. These are very slow
operations that take milliseconds. During this time the UDP client
software can't be looking for new messages coming in. What you need to
do is have your Client_OnRecieve methods quickly post your message to a
queue and leave so it can get back to looking for new messages. Then
have a background thread that de-queues messages and processes them.

Hope that helps
Leon Lambert
-------------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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top