By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Can't make UdpClient object send and receive broadcast messages

P: n/a
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:lin e 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.Soc ket,
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() ;
}
}

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Try adding the following to your Initialize() method:

socketObject.SetSocketOption(SocketOptionLevel.Soc ket,
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" wrote:
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:lin e 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.Soc ket,
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() ;
}
}

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.