473,396 Members | 1,703 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,396 software developers and data experts.

Udp receive problem

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 who answers the broadcast, so i know that it
sends a reply, but i cant figure out why my C# client cant receive it..

Any input on whats wrong with my code will be appreciated
//My code...
byte[] buffer = new byte[50];

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

socket.Bind(new IPEndPoint(IPAddress.Any, 0));

socket.Connect(new IPEndPoint(IPAddress.Broadcast, 4242));

socket.Send(System.Text.ASCIIEncoding.ASCII.GetByt es((char)2+"0"+(char)9+"1"
+(char)3));

socket.Receive(buffer, 0, 50, SocketFlags.None);

textBox1.Text = Encoding.ASCII.GetString(buffer);
Jul 4 '06 #1
2 5761
Daniel Lindberg wrote:
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 who answers the broadcast, so i know that
it sends a reply, but i cant figure out why my C# client cant receive it..

Any input on whats wrong with my code will be appreciated
//My code...
byte[] buffer = new byte[50];

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

socket.Bind(new IPEndPoint(IPAddress.Any, 0));

socket.Connect(new IPEndPoint(IPAddress.Broadcast, 4242));

socket.Send(System.Text.ASCIIEncoding.ASCII.GetByt es((char)2+"0"+(char
9+"1"
+(char)3));

socket.Receive(buffer, 0, 50, SocketFlags.None);

textBox1.Text = Encoding.ASCII.GetString(buffer);
Hi Daniel,

For UDP Broadcasting, you should be using the SendTo and ReceiveFrom
methods. You also shouldn't be connecting to anything.

The ReceiveFrom method takes an IPEndPoint as a ref parameter, which should
be initialised as:

EndPoint remoteEndPoint = new IPEndPoint( IPAddress.Broadcast, 4242 );

Then, call socket.ReceiveFrom using 'ref remoteEndPoint' as the parameter.
The remoteEndPoint object will also be modified to point to the endpoint
that the broadcast message originated from.

Here's a short example:

///
public void BroadcastTest ( )
{
// Instantiate
Socket sck = new Socket( AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp );

// Bind
sck.Bind( new IPEndPoint( IPAddress.Any, 0 ) );

// Broadcast ABCD
sck.SendTo( new byte[] { 0x41, 0x42, 0x43, 0x44 },
new IPEndPoint( IPAddress.Broadcast, 4242 ) );

// Receive on the broadcast IP, port 4242
EndPoint remoteEndPoint = new IPEndPoint( IPAddress.Broadcast, 4242 );

byte[] recvBuffer = new byte[128];
int bytesReceived = sck.ReceiveFrom( recvBuffer, ref remoteEndPoint );
}
///

Note: I typed this straight into KNode, I haven't tested it in the
slightest... but it just so happens that I've been coding some UDP
broadcast stuff lately, so this is fresh in my memory.

--
Hope this helps,
Tom Spink
Jul 4 '06 #2
I had to add this row before the sendto command:
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast,1);

But after that it worked like a charm. Thank you for your help!

"Tom Spink" <ts****@gmail.comskrev i meddelandet
news:uJ**************@TK2MSFTNGP03.phx.gbl...
Daniel Lindberg wrote:
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 who answers the broadcast, so i know that
it sends a reply, but i cant figure out why my C# client cant receive
it..

Any input on whats wrong with my code will be appreciated
//My code...
byte[] buffer = new byte[50];

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

socket.Bind(new IPEndPoint(IPAddress.Any, 0));

socket.Connect(new IPEndPoint(IPAddress.Broadcast, 4242));

socket.Send(System.Text.ASCIIEncoding.ASCII.GetByt es((char)2+"0"+(char
9+"1"
+(char)3));

socket.Receive(buffer, 0, 50, SocketFlags.None);

textBox1.Text = Encoding.ASCII.GetString(buffer);

Hi Daniel,

For UDP Broadcasting, you should be using the SendTo and ReceiveFrom
methods. You also shouldn't be connecting to anything.

The ReceiveFrom method takes an IPEndPoint as a ref parameter, which
should
be initialised as:

EndPoint remoteEndPoint = new IPEndPoint( IPAddress.Broadcast, 4242 );

Then, call socket.ReceiveFrom using 'ref remoteEndPoint' as the parameter.
The remoteEndPoint object will also be modified to point to the endpoint
that the broadcast message originated from.

Here's a short example:

///
public void BroadcastTest ( )
{
// Instantiate
Socket sck = new Socket( AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp );

// Bind
sck.Bind( new IPEndPoint( IPAddress.Any, 0 ) );

// Broadcast ABCD
sck.SendTo( new byte[] { 0x41, 0x42, 0x43, 0x44 },
new IPEndPoint( IPAddress.Broadcast, 4242 ) );

// Receive on the broadcast IP, port 4242
EndPoint remoteEndPoint = new IPEndPoint( IPAddress.Broadcast, 4242 );

byte[] recvBuffer = new byte[128];
int bytesReceived = sck.ReceiveFrom( recvBuffer, ref remoteEndPoint );
}
///

Note: I typed this straight into KNode, I haven't tested it in the
slightest... but it just so happens that I've been coding some UDP
broadcast stuff lately, so this is fresh in my memory.

--
Hope this helps,
Tom Spink

Jul 5 '06 #3

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

Similar topics

0
by: wang xiaoyu | last post by:
Hello,everyone. my program runs well in windows,i use tkSimpleDialog to receive some input,but when i copy my program into Linux RH8.0,entrys in my tkSimpleDialog derived Dialog have a vital...
4
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...
1
by: Kitchen Bin | last post by:
Hi. I am trying to use Sockets to do multiple Send and Receives via HTTP (not simultaneously). A first pair of Send/Receives works fine and sure enough I receive HTML back, but the next...
1
by: EppO | last post by:
I wrote a small server/client app. I created a class for my customized socket object using (synchronous) Sockets functions for both server and client use. When client connects to server, if It...
1
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...
0
by: whetham63 | last post by:
When I run this on my network at home and on my network at work it runs fine. When this is run at a clients network I am receiving a after the first packet is received(There are 2 packets...
0
by: Shoveler | last post by:
I've got an odd problem here that I've been beating my head on for days. I've written a class that uses a pre-established connection for communication, meaning I can use this class for a server or...
2
by: Rene Sørensen | last post by:
I'm using .NET 2.0 VS 2005 I'm creating a function that dos something similar to the. SmoApplication.EnumAvailableSqlServers() function. But for som resone I get an error or do i?. The problem...
0
by: ktg024 | last post by:
Hi all, I am beginner to VC++, I am trying to transmint and receive data through serial port using MSCOM. I am able to transmit data but I am unable to receive the data here is my receive...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.