Can't make UdpClient object send and receive broadcast messages

G

Guest

Please take a look at the simple code segment below and advise me what is
wrong.
According to the help and examples I've seen it should work unless I
misunderstand
something.

The problem is that UdpClient.Receive method always throws following
exception, even though
I verified that message was successfuly received by devices and responses
were sent back:

System.Net.Sockets.SocketException: A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 size,
SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP)
at Tester.CommonUITester.SendBroadcastCommandTest() in
c:\windows.net\cmi.ui\tester\commonuitester.cs:line 162The program '[1428]
Tester.exe' has exited with code 0 (0x0).


The code sample:
################

public class MyUdpClient : UdpClient
{
public MyUdpClient() : base()
{
}

public MyUdpClient(int nPort) : base(nPort)
{
}

~MyUdpClient()
{
Close() ;
}

public void Initialize()
{
Socket socketObject = this.Client;
socketObject.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 5000 );
}

};

class CommonUITester
{
private void SendBroadcastCommandTest()
{
RandomNumberGenerator rmGen = RandomNumberGenerator.Create();
byte [] rndBytes = new byte[2] ;
rmGen.GetNonZeroBytes(rndBytes ) ;

// Choose the port the device will send response to
int nPortNum = Utils.MakeWord(rndBytes[0], rndBytes[1] ) % 16000 + 49155;
// port number is from 49155 to 65155

MyUdpClient objUdpClient1 = new MyUdpClient() ;
objUdpClient1.Initialize() ;

// Replace 49154 with the port your device listen to.
IPEndPoint epBroadcastIP = new IPEndPoint(IPAddress.Broadcast, 49154);

// Set byteCommandBuffer to the data which your device understand.
//###############################################################
byte [] byteCommandBuffer = new byte[11];

byteCommandBuffer[0] = 13 ;
byteCommandBuffer[1] = 13 ;
byteCommandBuffer[2] = 0 ;
byteCommandBuffer[3] = (byte)Utils.HiByte(nPortNum);
byteCommandBuffer[4] = (byte)Utils.LoByte(nPortNum);
//###############################################################

try
{
objUdpClient1.Send(byteCommandBuffer, byteCommandBuffer.Length,
epBroadcastIP ) ;

// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

do
{
Thread.Sleep(0 ) ;

// Blocks until a message returns on this socket from a remote host.
byte[] receiveBytes = objUdpClient1.Receive(ref RemoteIpEndPoint);
}
while(true ) ;
}
catch(Exception e )
{
Debug.Write(e.ToString() ) ;
}

objUdpClient1.Close() ;
}

[STAThread]
static void Main(string[] args)
{
CommonUITester cmnUITester = new CommonUITester() ;

cmnUITester.SendBroadcastCommandTest() ;
}
}
 
G

Guest

Try adding the following to your Initialize() method:

socketObject.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);

NOTE - I am not 100% sure what the final value should be. It may be the
correct value is true.

HTH
Dan

Steve said:
Please take a look at the simple code segment below and advise me what is
wrong.
According to the help and examples I've seen it should work unless I
misunderstand
something.

The problem is that UdpClient.Receive method always throws following
exception, even though
I verified that message was successfuly received by devices and responses
were sent back:

System.Net.Sockets.SocketException: A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 size,
SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP)
at Tester.CommonUITester.SendBroadcastCommandTest() in
c:\windows.net\cmi.ui\tester\commonuitester.cs:line 162The program '[1428]
Tester.exe' has exited with code 0 (0x0).


The code sample:
################

public class MyUdpClient : UdpClient
{
public MyUdpClient() : base()
{
}

public MyUdpClient(int nPort) : base(nPort)
{
}

~MyUdpClient()
{
Close() ;
}

public void Initialize()
{
Socket socketObject = this.Client;
socketObject.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 5000 );
}

};

class CommonUITester
{
private void SendBroadcastCommandTest()
{
RandomNumberGenerator rmGen = RandomNumberGenerator.Create();
byte [] rndBytes = new byte[2] ;
rmGen.GetNonZeroBytes(rndBytes ) ;

// Choose the port the device will send response to
int nPortNum = Utils.MakeWord(rndBytes[0], rndBytes[1] ) % 16000 + 49155;
// port number is from 49155 to 65155

MyUdpClient objUdpClient1 = new MyUdpClient() ;
objUdpClient1.Initialize() ;

// Replace 49154 with the port your device listen to.
IPEndPoint epBroadcastIP = new IPEndPoint(IPAddress.Broadcast, 49154);

// Set byteCommandBuffer to the data which your device understand.
//###############################################################
byte [] byteCommandBuffer = new byte[11];

byteCommandBuffer[0] = 13 ;
byteCommandBuffer[1] = 13 ;
byteCommandBuffer[2] = 0 ;
byteCommandBuffer[3] = (byte)Utils.HiByte(nPortNum);
byteCommandBuffer[4] = (byte)Utils.LoByte(nPortNum);
//###############################################################

try
{
objUdpClient1.Send(byteCommandBuffer, byteCommandBuffer.Length,
epBroadcastIP ) ;

// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

do
{
Thread.Sleep(0 ) ;

// Blocks until a message returns on this socket from a remote host.
byte[] receiveBytes = objUdpClient1.Receive(ref RemoteIpEndPoint);
}
while(true ) ;
}
catch(Exception e )
{
Debug.Write(e.ToString() ) ;
}

objUdpClient1.Close() ;
}

[STAThread]
static void Main(string[] args)
{
CommonUITester cmnUITester = new CommonUITester() ;

cmnUITester.SendBroadcastCommandTest() ;
}
}
 

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