O
OBones
Hi all,
I'm currently trying to receive replies to a UDP broadcast that I sent
and I can't get it to work. I know the datagrams are sent as I see
them with WireShark, but my C# code does not see them.
Here is how I intended things to work:
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
try
{
// Send data to the broadcast address
IPEndPoint broadcastIEP = new IPEndPoint(IPAddress.Broadcast,
port);
socket.Connect(broadcastIEP);
socket.Send(data);
// Wait for someone to respond
for (int i = 0; i < 50; i++)
{
int availableBytes = socket.Available;
if (availableBytes > 0)
{
byte[] buffer = new byte[availableBytes];
socket.Receive(buffer, 0, availableBytes,
SocketFlags.None);
return ASCIIEncoding.ASCII.GetString(buffer);
}
System.Threading.Thread.Sleep(100);
}
return "Nobody replied";
}
finally
{
socket.Close();
}
The intent is to send a UDP broadcast and wait for the first machine
that replies. The wait should stop after a while.
I must be doing something wrong here, but I'm quite lost as to what I
should be doing. I tried "ReceiveFrom", but it is a blocking call and
it never seems to return. I see the datagrams coming through (using
WireShark) but ReceiveFrom does not return.
Thanks for your help
Cheers
Olivier
I'm currently trying to receive replies to a UDP broadcast that I sent
and I can't get it to work. I know the datagrams are sent as I see
them with WireShark, but my C# code does not see them.
Here is how I intended things to work:
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
try
{
// Send data to the broadcast address
IPEndPoint broadcastIEP = new IPEndPoint(IPAddress.Broadcast,
port);
socket.Connect(broadcastIEP);
socket.Send(data);
// Wait for someone to respond
for (int i = 0; i < 50; i++)
{
int availableBytes = socket.Available;
if (availableBytes > 0)
{
byte[] buffer = new byte[availableBytes];
socket.Receive(buffer, 0, availableBytes,
SocketFlags.None);
return ASCIIEncoding.ASCII.GetString(buffer);
}
System.Threading.Thread.Sleep(100);
}
return "Nobody replied";
}
finally
{
socket.Close();
}
The intent is to send a UDP broadcast and wait for the first machine
that replies. The wait should stop after a while.
I must be doing something wrong here, but I'm quite lost as to what I
should be doing. I tried "ReceiveFrom", but it is a blocking call and
it never seems to return. I see the datagrams coming through (using
WireShark) but ReceiveFrom does not return.
Thanks for your help
Cheers
Olivier