- Home /
C# code run in App Console but not on Unity console
Hi guys! First, sorry for my english. Now, I have a problem with a code which uses sockets. I want connect my script over IPv6 but I receive a SocketException when I run this script in Unity. The next code works perfect as a Console Application Project in MonoDevelop.
Socket s;
s = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
IPAddress ip = IPAddress.Parse("ff15::2");
s.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(ip));
IPEndPoint ipep = new IPEndPoint(IPAddress.IPv6Any, 26000);
s.Bind(ipep);
while (true) {
byte[] b = new byte[1024];
if (s == null)
Console.WriteLine ("S es null");
else
s.Receive (b);
string str = System.Text.Encoding.ASCII.GetString (b, 0, b.Length);
Console.WriteLine (str.Trim ());
}
}
But the same code (I only changed "Console.WriteLine()" for "Debug.Log()") doesn't work as Unity Project. This code break with the exception: "SocketException: An address incompatible with the requested protocol was used." Can someone help me? Thanks!
Santiago.
Is this for web player? I don't think the security sandbox will allow you to create listening sockets there.
What does it say on the top of your code, where it says using ....
Sorry, I forgot add the libraries at the top of the code:
using UnityEngine; using System.Net; using System.Net.Sockets; using System.Net.NetworkInformation;
Having almost same issue.
m_socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.Add$$anonymous$$embership, new IPv6$$anonymous$$ulticastOption(IPAddress.Parse("FF02::1")));
SocketException: The requested address is not valid in its context.
System.Net.Sockets.Socket.SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, System.Object optionValue)
$$anonymous$$essage: The requested address is not valid in its context.
Source: System
TargetSite: Void SetSocketOption(SocketOptionLevel, SocketOptionName, System.Object)
ErrorCode: 10049
NaticeErrorCode: 10049
SocketErrorCode: AddressNotAvailable
Perfectly works in Console application, does not work in Unity.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163620im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
UDPSend not working with iPad over LAN 0 Answers
UDP implementation in Unity, unable to send to two different machines 0 Answers
Receiving UDP data in Unity? 0 Answers