[Update: Looks Mike, future PM of System.Net and all ’round genius, cleared things up in the comments. Thanks, Mike. He gets 20 jasonkemp.ca points! Save those up and you may get a toaster. Or a beer since I know you and you live in Victoria. Incidently, before I checked for comments to this post, I opened up MSDN help, typed in “socket“ without quotes and no filter, and found the error codes in a few links. Who knew? 🙂 Now we just have to correct that documentation UdpClient.]
So I’m trying to play with UdpClient from the System.Net.Sockets namespace in good ol’ .NET 1.1.
I’m using the code from the Receive() example in MSDN Help. Almost exactly. Only in my version, the fucking thing always throws a SocketException. Luckily the SocketExpection has an ErrorCode property so that I can “refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.” And yet, I still don’t know what’s going wrong because as far as my local MSDN Library, MSDN for the Internet, and Google are concerned there is no such fucking Windows Socket Version 2 API error code documentation. (In fact, if Google is worth its salt, this post will begin coming up when that string is searched.) All I can use to debug is the stack trace. And it’s not my code that’s being difficult so break points won’t work. Could it be XP SP2? Don’t know; ’cause I can’t for the life of me find that info on MSDN either. I know it’s not Windows Firewall because I tried this code with the Firewall turned off. I haven’t used the excellent Reflector on this either, but I don’t see how that’ll help right now. And I know how to use Google, in case you were wondering! Sometimes Microsoft really pisses me off.
1: [STAThread]
2: static void Main(string[] args)
3: {
4: UdpClient udpClient = new UdpClient();
5: Â
6: IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
7: try
8: {
9: byte[] received = udpClient.Receive( ref endPoint );
10: Â
11: string recvMessage = Encoding.ASCII.GetString(received);
12: Â
13: Console.WriteLine("recvMessage = {0}", recvMessage);
14: }
15: catch ( SocketException e)
16: {
17: Console.WriteLine(e);
18: }
19: catch( Exception e )
20: {
21: Console.Out.WriteLine("e = {0}", e);
22: }
23: Â
24: Console.ReadLine();
25: }
It always throws an exception on line 9. Can anyone tell me why this is failing? 10 jasonkemp.ca points, and my eternal gratitude, to anyone who can. 10 jasonkemp.ca points to anyone can point to good Winsock 2 API documenation also.
Winsock 2 error codes can be found here:
http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp
I haven’t look at your code yet… but in the meantime this may help 🙂
try something like: (I haven’t the code below, but I think it should work)
UdpClient c = new UdpClient(5001);
IPEndPoint RemoteEndPoint = null;
…
byte[] receivedBytes = c.Receive(ref RemoteEndPoint);
…
// you have to listen on a port, typically greater than 1024
woohoo…. 20 jk.ca points – just wait until I tell all my friends 🙂
I found this on google btw.
Along with the link of what I was looking for.