我使用套接字的某些代碼存在問題。我想通過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."
有人可以幫助我嗎?謝謝!