我在c#中编写了一个udp发送和接收unity3d插件dll,但它无法从iphone4s上的服务器接收任何udp数据包。
当服务器向在 iphone4s 上运行的 demo 发送 udp 数据包时,它无法接收任何内容。
当服务器向在 ipod touch 4 上运行的 demo 发送 udp 数据包时,它可以工作!收到数据包!
当服务器向 Unity3d 编辑器中运行的演示发送 udp 数据包时,它可以正常工作!
我使用Wireshark捕获客户端和服务器之间的数据包,它确实捕获了服务器发送给演示的数据包,但是客户端演示似乎没有收到任何信息。
为什么?
internal class RudpNetInteractor
{
private Socket socket;
private EndPoint serverAddress;
internal RudpNetInteractor(IPEndPoint serverAddress)
{
this.rudpClient = rudpClient;
this.serverAddress = serverAddress;
}
internal void BuildConnection()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Connect(serverAddress);
BeginReceive();
}
internal void Send(byte[] buffer)
{
socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
}
private void OnSend(IAsyncResult ar)
{
try
{
socket.EndSend(ar);
}
catch {}
}
internal void Destroy()
{
socket.Close();
socket = null;
}
private void OnReceive(IAsyncResult ar)
{
try
{
SocketReceiveInfo receiveInfo = (SocketReceiveInfo)ar.AsyncState;
Socket receiveSocket = receiveInfo.ReceiveSocket;
int len = receiveSocket.EndReceive(ar);
if (len > 0)
{
byte[] dest = new byte[len];
Array.Copy(receiveInfo.ReceiveBytes, dest, len);
Console.WriteLine(BitConverter.ToString(dest));
}
BeginReceive();
}
catch {}
}
private void BeginReceive()
{
byte[] receiveBytes = new byte[576];
SocketReceiveInfo info = new SocketReceiveInfo(receiveBytes, socket);
socket.BeginReceiveFrom(receiveBytes, 0, receiveBytes.Length, SocketFlags.None, ref serverAddress, new AsyncCallback(OnReceive), info);
}
}
internal class SocketReceiveInfo
{
private byte[] receiveBytes;
private Socket receiveSocket;
internal SocketReceiveInfo(byte[] receiveBytes, Socket receiveSocket)
{
this.receiveBytes = receiveBytes;
this.receiveSocket = receiveSocket;
}
internal byte[] ReceiveBytes
{
get { return receiveBytes; }
}
internal Socket ReceiveSocket
{
get { return receiveSocket; }
}
}