473,549 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't make UdpClient object send and receive broadcast messages

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.Recei ve method always throws following
exception, even though
I verified that message was successfuly received by devices and responses
were sent back:

System.Net.Sock ets.SocketExcep tion: 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.Sock ets.Socket.Rece iveFrom(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sock ets.Socket.Rece iveFrom(Byte[] buffer, Int32 size,
SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sock ets.UdpClient.R eceive(IPEndPoi nt& remoteEP)
at Tester.CommonUI Tester.SendBroa dcastCommandTes t() in
c:\windows.net\ cmi.ui\tester\c ommonuitester.c s: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.Se tSocketOption(S ocketOptionLeve l.Socket,
SocketOptionNam e.ReceiveTimeou t, 5000 );
}

};

class CommonUITester
{
private void SendBroadcastCo mmandTest()
{
RandomNumberGen erator rmGen = RandomNumberGen erator.Create() ;
byte [] rndBytes = new byte[2] ;
rmGen.GetNonZer oBytes(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.I nitialize() ;

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

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

byteCommandBuff er[0] = 13 ;
byteCommandBuff er[1] = 13 ;
byteCommandBuff er[2] = 0 ;
byteCommandBuff er[3] = (byte)Utils.HiB yte(nPortNum);
byteCommandBuff er[4] = (byte)Utils.LoB yte(nPortNum);
//############### ############### ############### ############### ###

try
{
objUdpClient1.S end(byteCommand Buffer, byteCommandBuff er.Length,
epBroadcastIP ) ;

// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoin t = new IPEndPoint(IPAd dress.Any, 0);

do
{
Thread.Sleep(0 ) ;

// Blocks until a message returns on this socket from a remote host.
byte[] receiveBytes = objUdpClient1.R eceive(ref RemoteIpEndPoin t);
}
while(true ) ;
}
catch(Exception e )
{
Debug.Write(e.T oString() ) ;
}

objUdpClient1.C lose() ;
}

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

cmnUITester.Sen dBroadcastComma ndTest() ;
}
}

Nov 17 '05 #1
1 7812
Try adding the following to your Initialize() method:

socketObject.Se tSocketOption(S ocketOptionLeve l.Socket,
SocketOptionNam e.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.Recei ve method always throws following
exception, even though
I verified that message was successfuly received by devices and responses
were sent back:

System.Net.Sock ets.SocketExcep tion: 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.Sock ets.Socket.Rece iveFrom(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sock ets.Socket.Rece iveFrom(Byte[] buffer, Int32 size,
SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sock ets.UdpClient.R eceive(IPEndPoi nt& remoteEP)
at Tester.CommonUI Tester.SendBroa dcastCommandTes t() in
c:\windows.net\ cmi.ui\tester\c ommonuitester.c s: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.Se tSocketOption(S ocketOptionLeve l.Socket,
SocketOptionNam e.ReceiveTimeou t, 5000 );
}

};

class CommonUITester
{
private void SendBroadcastCo mmandTest()
{
RandomNumberGen erator rmGen = RandomNumberGen erator.Create() ;
byte [] rndBytes = new byte[2] ;
rmGen.GetNonZer oBytes(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.I nitialize() ;

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

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

byteCommandBuff er[0] = 13 ;
byteCommandBuff er[1] = 13 ;
byteCommandBuff er[2] = 0 ;
byteCommandBuff er[3] = (byte)Utils.HiB yte(nPortNum);
byteCommandBuff er[4] = (byte)Utils.LoB yte(nPortNum);
//############### ############### ############### ############### ###

try
{
objUdpClient1.S end(byteCommand Buffer, byteCommandBuff er.Length,
epBroadcastIP ) ;

// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoin t = new IPEndPoint(IPAd dress.Any, 0);

do
{
Thread.Sleep(0 ) ;

// Blocks until a message returns on this socket from a remote host.
byte[] receiveBytes = objUdpClient1.R eceive(ref RemoteIpEndPoin t);
}
while(true ) ;
}
catch(Exception e )
{
Debug.Write(e.T oString() ) ;
}

objUdpClient1.C lose() ;
}

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

cmnUITester.Sen dBroadcastComma ndTest() ;
}
}

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
16002
by: Morten Overgaard | last post by:
Hi I'm listening on the SysLog port (514) through UDP. The problem is that I am not receiving anything nut I know that i get messages on the port. When I use KIWI to listen on the same port via UDP tons of messages arrive.. What am I'm doing wrong My code looks like the following... private UdpClient udpClient;
1
3925
by: Dan Kelley | last post by:
I have 2 projects - 1 Winform project that sends Udp messages using the UdpClient class when a button is clicked, and a Console application that listens for these Udp messages. If I try to use the UdpClient class to receive, it never works. The messages are never received (no exceptions). If I use a normal socket configured to listen for...
2
11886
by: Rick | last post by:
Hi, I'm trying to get a simple UdpClient app working. I've been looking at the MSDN info regarding UdpClient. When I set it up on my own PC and send messages to myself it works OK. If I try to send messages to another PC with another IP address I get multiple exception, the first one being "The requested address is not valid in its...
2
9514
by: goodmannewz | last post by:
I just cannot run the sample code from msdn. Please tell me why? Thanks a lot. I create a console C# project, parsed the example codes: public class UDPMulticastListener { private static readonly IPAddress GroupAddress = IPAddress.Parse("192.168.0.1"); private const int GroupPort = 9200; private static void StartListener()
0
1225
by: Steve | last post by:
I am looking into UdpClient sample code from MSDN where one UdpClient object used for both send and receive operation. Please take a look at the link below http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetsocketsudpclientclasstopic.asp I can't make similar to this code work. The udpClient.Receive...
0
2197
by: tccode97 | last post by:
To whom it may concern, I am developing a socket application in VC++ that uses asynchronous connnection. After doing search on google, I found the following link http://msdn2.microsoft.com/en-us/library/system.net.sockets.udpclient.beginreceive.aspx First of all, thanks for microsoft team for this post.
1
2512
by: tccode97 | last post by:
Hi, I need an urgent help. I am developing a socket application in VC++ that uses asynchronous connnection. After doing search on google, I found the following link http://msdn2.microsoft.com/en-us/library/system.net.sockets.udpclient...
1
3562
by: greenxiar | last post by:
My code is below and the platform is Win2K3 R2: using System; using System.Net; using System.Net.Sockets; using System.Threading; namespace TechUDPBroadcast { class Program { static void Main(string args) {
0
1018
by: Karl Durrance | last post by:
Hi all, I am having a weird issue with sending and receiving multicast messages either using the UDPClient object or directly with sockets. Normally, my application works perfectly, receiving all messages async through the BeginRecieve() method of the UDPClient. But in some circumstances, for instance while connected to a Cisco VPN, my...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.