Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I performed the Microsoft Udp examples successfully at :
http://msdn.microsoft.com/en-us/library/tst0kwb1.aspx

Neither of these two applications use "Bind".

My application throws an error if I do not use Bind:
ERROR: "You must call the Bind method before performing this operation."

int UdpRxPort = 8000;//-default, change next line.....
int.TryParse(textBoxPort.Text, out UdpRxPort);

//-ip address
IPAddress udpAddress = IPAddress.Parse(comboBoxIPaddress.Text);

//-MultiCast or Broadcast : Multi = 224.0.0.0 -> 239.255.255.255--------[multi/Broad test]
//////////////////////////////////////////////////////////////////////////////////////////
string addrStr = comboBoxIPaddress.Text.Substring(0, comboBoxIPaddress.Text.IndexOf("."));
//-here, we've taken the first digits up until the dot '.'
// from the addr : ###.###.###.### is now first ### ex. 192.168.1.25 = 192 or 233.200.132.92 = 233
//- this will tell us if we are broadcast or multicast by looking at the first 3 digits
int sampleIP = 0;
int.TryParse(addrStr, out sampleIP);

//-now test for udp type : multicast or broadcast
bool isMultiCastAddr = (sampleIP >= 224 && sampleIP < 240) ? true : false;
//-test complete, lets go............

//-create a new udp listener
udpClientRx = new UdpClient();
//-Allow another client to bind to this port.
udpClientRx.ExclusiveAddressUse = false;

//udpClientRx.Client.Bind(new IPEndPoint(IPAddress.Any, UdpRxPort));

if (isMultiCastAddr == false)//-Broadcast
{
udpClientRx.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

//-MUST USE BIND ?
udpClientRx.Client.Bind(new IPEndPoint(IPAddress.Any, UdpRxPort));
}
else//-yes, this is a multicast addr so join here
{
udpClientRx.JoinMulticastGroup(udpAddress);

//-MUST USE BIND ?
udpClientRx.Client.Bind(new IPEndPoint(IPAddress.Any, UdpRxPort));
}

//-Go UDP Rx-----------------------------------------------------[Async Rx]
udpClientRx.BeginReceive(new AsyncCallback(ReadDataCallBack), udpClientRx);

So why do I have to use "bind" if the Microsoft examples don't use it ?
Posted

On the second line of code you do not asign int.Parse to UdpRxPort. Not sure if that is the solution to your problem or just an error when pasting your code. int.Parse will not fail even when it is not assigned to any variable at all (since it can be used in a logical statement).
 
Share this answer
 
C#
string addrStr = comboBoxIPaddress.Text.Substring(0, comboBoxIPaddress.Text.IndexOf("."));
 //-here, we've taken the first digits up until the dot '.'
 // from the addr : ###.###.###.### is now first ### ex. 192.168.1.25 = 192 or 233.200.132.92 = 233
 //- this will tell us if we are broadcast or multicast by looking at the first 3 digits
 int sampleIP = 0;
 int.TryParse(addrStr, out sampleIP);


That seams to work for me, I'm getting the correct sampleIP from "out sampleIP" in the int.TryParse method.

I will get an error when I comment out the :

udpClientRx.Client.Bind(new IPEndPoint(IPAddress.Any, UdpRxPort));

And, after studying this, I think it might be that I'm setting up a listener but the Microsoft code examples for send and receive work well with out the "Bind"'s.

Very confused at this point......
 
Share this answer
 
This is the Microsoft tutorial. Notice it does not use "Bind" (and runs fine on the same PC)

UDP Listener:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class UDPListener
{
private const int listenPort = 11000;

private static void StartListener()
{
bool done = false;

UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any,listenPort);

try
{
while (!done)
{
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive( ref groupEP);

Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes,0,bytes.Length));
}

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
}
}

public static int Main()
{
StartListener();

return 0;
}
}

UDP Receiver

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class Program
{
static void Main(string[] args)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

IPAddress broadcast = IPAddress.Parse("192.168.1.255");

byte[] sendbuf = Encoding.ASCII.GetBytes(args[0]);
IPEndPoint ep = new IPEndPoint(broadcast, 11000);

s.SendTo(sendbuf, ep);

Console.WriteLine("Message sent to the broadcast address");
}
}

Yet if I do not use Bind in my receiver, it errors out with:
ERROR: "You must call the Bind method before performing this operation."
 
Share this answer
 
Comments
Kirill Borunov 23-Sep-13 9:06am    
UdpClient listener = new UdpClient(listenPort);
i think passing the listen port to constructor is the same as using the bing method.
OK, found a really great explination at:

http://codeidol.com/community/dotnet/a-simple-udp-application/10474/[^]

For connectionless communications, you must specify the Dgram SocketType, along with the Udp ProtocolType. Remember, if your application does not need to receive UDP data on a specific UDP port, you do not have to bind the socket to a specific IPEndPoint. However, if you do need to listen to specific port, such as for servers, you must use the Bind() method.

Now I see that since I'm receiving, I have to "Bind".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900