trying out UDP socket, and failing.... (on Vista)

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

I think Vista might be the culprit....
Anyway, could anyone share with me how to send some data over UDP and
recieve it?
Now it's not working, and I have no clue if it's the sending or recieving or
both which fails....

Anyway I have simple code like that:

=== send ====
public void Send(byte[] data, IPEndPoint to)
{
using (Socket sock = new Socket(to.AddressFamily, SocketType.Dgram,
ProtocolType.Udp))
sock.SendTo(data, SocketFlags.None, to);
}


=== recieve ===
void Receiving()
{
byte[] rbuf = new byte[1 << 14];
using (Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp))
while (true)
{
EndPoint rep = new IPEndPoint(IPAddress.Any, 0);
int read = sock.ReceiveFrom(rbuf, ref rep);
Recieved(rbuf, read, rep);
}
}
void Recieved(byte[] buf, int nRead, EndPoint where)
{
Console.WriteLine("Recieved {0} byte(s) from {1}", nRead, where);
}

===========
 
Never mind,I found it!
I should also (in the listening code)

sock.Bind(new IPEndPoint(IPAddress.Any, aPort))


Pay attention to 'aPort' I should use the same port for target IPEndPoint
when sending data
 
Thanks for correcting me on Receive, I hought it was the contrary (Recieve)
for a long time now!
Other than that I like to define size as left shift, a matter of habit I
suppose.

In fact it was my bug and a strange "Vista issue".

As soon I thought of trying my program as an administrator, it threw an
error which show me the problem:
sock.Bind() should be called prior to sock.RecieveFrom()

As to why it fails silently on Vista when ran as user, it is a mystery!
 
Back
Top