Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by balt · May 05, 2012 at 02:08 AM · socketsudp

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);
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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");
 // [...]
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 05, 2012 at 03:12 AM 0
Share

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

avatar image balt · May 05, 2012 at 06:47 AM 0
Share

Excellent, this works now. Your broadcast address however is going to broadcast a whole lot further than necessary for same subnet messaging... :-)

avatar image Bunny83 · May 05, 2012 at 10:58 AM 0
Share

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 ;)

avatar image Qertyfantus · Dec 23, 2014 at 02:17 AM 0
Share

awesome,it works.now android could send udp broadcast but i still have a problem.it looks like android couldnot receive broadcast message.

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cdadr · Jun 28, 2018 at 02:16 PM 0
Share

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.

avatar image
0

Answer by sandyakayika5730 · Sep 25, 2020 at 06:18 PM

1024 @LupaNafas

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges