473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem with broadcast in C#

Can anybody help and explain why?
I wrote two programs just to test broadcasting in C#, which both run on the
same machine. However, the receiver program can't receive any messages from
the sender program. Key parts of the source codes are listed below:

/****** Sender.cs *****/
int remotePort = 8888;
// Init the socket
try {
Socket sendSocket = new Socket( AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp );
sendSocket.SetSocketOption( SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
IPEndPoint remoteEP = new IPEndPoint( IPAddress.Broadcast, remotePort );
}
catch (Exception e ) {
Console.WriteLine( e.ToString() );
}

while ( true ) {
sendSocket.SendTo( data, remoteEP );
Thread.CurrentThread.Join( new TimeSpan( 0, 0, 0, 5, 0 ) );
}

/****** Listener.cs *****/
byte[] data = new byte[128];
localPort = 8888;
try{
IPEndPoint remoteEP = new IPEndPoint( new IPAddress( 0 ), 0 );
UdpClient udpListener = new UdpClient( localPort );
}
catch ( Exception e ) {
Console.WriteLine( e.ToString() );
}

while ( true ) {
data = udpListener.Receive( ref remoteEP );
}

Thank you ! LOL~
Nov 17 '05 #1
3 6623
Broadcast works, we use it often in our networked medical systems.

When you create your listening remoteEP, try using the loopback IP
"127.0.0.1" and see what happens. My understanding is that Broadcast
only works within the scope of a subnet, and unless your network is
set up on "0.0.0.xxx" then you're trying to listen on an invalid
address.

For instance, if your network card IP is "192.168.0.1" then you are
broadcasting to "192.168.0.xxx" range of addresses...but trying to
listen on "0.0.0.xxx" with the code that you show won't receive it.

I'll check some working code after lunch and let you know how we do
it, but your really close.

A test would be to set the sending & receiving IP's to a known and
validate it works, then only change the sending to Broadcast to see it
still works, then try variations of your listening IP.

HTH,
Joe
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 17 '05 #2
Ok, I was wrong. I have a test app that received OK with your "new
IPAddress(0)"...don't know why, but it did.

I haven't used the UDPClient, I just open a second socket to listen,
same as the first, but I "bind" it to a remote EP in order to
listen.
iepRx = new IPEndPoint ( new IPAddress( 0 ), nDataPort
);
epRx = (EndPoint) iepRx;

sckRx = new Socket ( AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp );
sckRx.SetSocketOption ( SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 500 );
sckRx.Bind ( epRx );
I've used the IPAddress.Broadcast to send, and I've also listened on
the IPAddress.Any and they work as expected. But, as I said, I tried
address 0, and it worked too.

Here is the test app I used, maybe it will help. Note the casting of
the IPEndpoint to an EndPoint for the bind call.

http://www.cometracing.com/Loopback/Loopback.zip

Good Luck,
Joe
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 17 '05 #3
Thank you, Joe!

I've put the two programs to seperate computers in the same subnet, and it
works as you mentioned. The reason that the datagrams won't be received on
the same machine is that I didn't specify the loop option, also as you
pointed out.

I am new to network programming and will surely meet with all kinds of
problems that may seem too simple, but I really appreciate you and other
people's help.

Thanks again!

"CometJoe" wrote:
Ok, I was wrong. I have a test app that received OK with your "new
IPAddress(0)"...don't know why, but it did.

I haven't used the UDPClient, I just open a second socket to listen,
same as the first, but I "bind" it to a remote EP in order to
listen.
iepRx = new IPEndPoint ( new IPAddress( 0 ), nDataPort
);
epRx = (EndPoint) iepRx;

sckRx = new Socket ( AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp );
sckRx.SetSocketOption ( SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 500 );
sckRx.Bind ( epRx );
I've used the IPAddress.Broadcast to send, and I've also listened on
the IPAddress.Any and they work as expected. But, as I said, I tried
address 0, and it worked too.

Here is the test app I used, maybe it will help. Note the casting of
the IPEndpoint to an EndPoint for the bind call.

http://www.cometracing.com/Loopback/Loopback.zip

Good Luck,
Joe
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 17 '05 #4

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

Similar topics

2
by: Mark van Heeswijk | last post by:
Hi all, currently i'm working on a client-server application... when doing an UDP-broadcast the packet is received ok, at the other side, but for some reason after that _ANOTHER_ packet is...
2
by: Andrew Rawnsley | last post by:
Downloaded the eRServer 1.2 source (thanks, guys) and installed, and am having a problem. Anyone else dug into this yet? Situation: PG 7.3.4 on linux (master) Its been tried with 3 slaves,...
4
by: Cott Lang | last post by:
ERROR: could not convert UTF-8 character 0x00ef to ISO8859-1 Running 7.4.5, I frequently get this error, and ONLY on this particular character despite seeing quite a bit of 8 bit. I don't really...
9
by: mupe | last post by:
Hi, i have a problem with a Type Library, which is written in C++. I am developing an application in C#.NET and have to use functions from this COM-Type Library. When I use these functions in...
7
by: Mike Davies | last post by:
Hi, I'm developing an APP which pushed web content to various devices on a LAN (these devices happen to be IP phones). I need to broadcast a HTTP message to maybe 100 phones at a time but...
2
by: Gunnar_Frenzel | last post by:
Hello, this might be an easy question, but I don't have any handy solution at hand. I have an application that is supposed to send UDP broadcast. So far so easy, I did: Socket sockSendBroadcast...
2
by: Daniel Lindberg | last post by:
Hi, im a beginner in C# and i just cant seem to get my udp broadcasting to work. What happens is that it just locks down on the Receive call and never gets any data. I have the code for the server...
0
by: salman | last post by:
hello i have client network (LAN) connected with server, and some client connected to server via RAS (Remote Access Service) and makes VPN. My problem is that when i broadcast message to...
9
by: Irmen de Jong | last post by:
Hello Sorry this might be a bit offtopic but I don't really know where else to post this question. If you could point me in the right direction that is much appreciated. I'm running into a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.