【发布时间】:2014-09-25 19:23:21
【问题描述】:
我对一些使用套接字的代码有疑问。我想通过 IPv6 连接我的脚本,但是当我在 Unity 中运行此脚本时收到 SocketException。此代码在 MonoDevelop 中完美地作为控制台应用程序项目:
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()")不能作为 Unity 项目工作。此代码因异常而中断:"SocketException: An address incompatible with the requested protocol was used." 有人可以帮助我吗?谢谢!
【问题讨论】:
标签: c# unity3d mono ipv6 socketexception