- Home /
Socket exception: Access denied
Hi all,
I want to use System.Net
and System.Net.Sockets
to broadcast UDP packets on my LAN, however, on sending the data on the socket I get an "Access denied" exception. I'm new to .Net and C#, am I doing something wrong programming wise, or is this perhaps a licensing limitation (I'm using an expired unity pro trial license, which I believe reverts to the same functionality as the free version?)
Here's the code:
using System.Net;
using System.Net.Sockets;
using System.Text;
Boolean exception_thrown = false;
Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress send_to_address = IPAddress.Parse("192.168.0.255");
IPEndPoint sending_end_point = new IPEndPoint(send_to_address, 25000);
string formattedString;
byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
formattedString = String.Format("sending to address: {0} port: {1}", sending_end_point.Address, sending_end_point.Port);
try {
Debug.Log(send_buffer);
sending_socket.SendTo(send_buffer, sending_end_point);
} catch (Exception send_exception ) {
exception_thrown = true;
formattedString = String.Format("Exception {0}", send_exception.Message);
Debug.Log(formattedString);
}
Answer by Bunny83 · May 05, 2012 at 02:51 AM
I just tried your code together with one of my old UDP listener (written in Delphi) and i receive the text i send out to my local broadcast address. Are you sure that you build to standalone? It won't work in a web build of course...
edit
I've played a bit with some parameters and i've found out that you can't send broadcasts to the general broadcast address unless you set EnableBroadcast to true.
So just set the property to true right after you created your socket:
Socket sending_socket = new Socket(/*[...]*/);
sending_socket.EnableBroadcast = true;
IPAddress send_to_address = IPAddress.Parse("255.255.255.255");
// [...]
If i try the same with the platform switched to webplayer (in the build settings) i get an exception:
Exception SendTo request refused by Unity webplayer security model
Excellent, this works now. Your broadcast address however is going to broadcast a whole lot further than necessary for same subnet messaging... :-)
Sure but why do you bother? ;) Usually you use a broadcast to find other clients that are reachable. Broadcasts aren't routed through the internet so usually you only reach local area hosts and only those that are reachable due to routine tables, otherwise they get stuck at the next switch / router ;).
If this should be a game that works in all LANs you don't know the local subnet without a query. If you do this only for you, well it doesn't matter, but it'S still prone to error when you get a new router with a new subnet (and you don't change it ;))
For example your address doesn't work in my case, i use a different local subnet ;)
awesome,it works.now android could send udp broadcast but i still have a problem.it looks like android couldnot receive broadcast message.
Answer by tgraupmann · Apr 09, 2013 at 08:30 PM
On Mac, you have to run Unity with elevated permissions to host sockets. Try the following command:
sudo /Applications/Unity/Unity.app/Contents/MacOS/Unity
This answer is only true for port numbers less than 1024.
For ports 1024 and higher, you can host as a normal user, but 1023 and less, you will need to use sudo.
Your answer
Follow this Question
Related Questions
UDP Sockets for networking, getting overloaded?? 0 Answers
Why breaks my Ethernet over USB (USB NCM) connection between HoloLens and PC? 0 Answers
getting udp package info inside unity (GlovePIE) 0 Answers
Problems with UDP data sending 1 Answer
I cant read from the sockets properly - exception 0x80004005: An invalid argument was supplied 2 Answers