我使用套接字的某些代码存在问题。我想通过IPv6连接我的脚本,但在Unity中运行此脚本时收到一个SocketException。此代码工作完全作为一个控制台应用程序项目的MonoDevelop:IPv6套接字在Mono控制台应用程序中可用,但不适用于Unity应用程序
using System;
using System.Net;
using System.Net.Sockets;
namespace socketIPv6
{
class MainClass
{
public static void Main (string[] args)
{
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];
s.Receive (b);
string str = System.Text.Encoding.ASCII.GetString (b, 0, b.Length);
Console.WriteLine (str.Trim());
}
}
}
}
但相同的代码(我只改"Console.WriteLine()"
为"Debug.Log()"
)不作为统一的项目工作。此代码打破了例外:"SocketException: An address incompatible with the requested protocol was used."
有人可以帮助我吗?谢谢!