当我运行游戏时,我的UDP服务器导致编辑器冻结(游戏从不启动)。它没有到达Debug.Log(),所以我尝试删除某些行,仅在执行Socket.Bind()时,编辑器才会冻结。我还在另一个脚本中运行UDP客户端,不确定是否会造成干扰。
private int _recv;
private byte[] _data;
private IPEndPoint _ipep;
private Socket _socket;
private IPEndPoint _sender;
private EndPoint _remote;
void Start () {
_ipep = new IPEndPoint(IPAddress.Any, 6969);
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_socket.Bind(_ipep);
Debug.Log("Waiting for a client...");
_sender = new IPEndPoint(IPAddress.Any, 0);
_remote = (EndPoint) (_sender);
}
void Update () {
_data = new byte[1024];
_recv = _socket.ReceiveFrom(_data, ref _remote);
Debug.Log(Encoding.ASCII.GetString(_data, 0, _recv));
}
我正在使用Unity 2017.4.6,也尝试过使用Unity 2018,但没有效果。
谢谢!