none
使用Unity开发UWP游戏 , StreamSocket 无法正常接收数据 RRS feed

  • 问题

  • 在使用Unity3d 5.3.4进行UWP网络游戏开发的过程中,使用如下代码进行接收数据时,出现内存不足的异常,异常信息如下:

    Exception thrown: 'System.OutOfMemoryException' in Assembly-CSharp.dll
    Received data: "Read stream failed with error: Insufficient memory to continue the execution of the program."

    private StreamSocket client = new StreamSocket();

    //连接到服务器

    public async void Connect(string ip, int port) { try { var serverHost = new HostName(ip); await client.ConnectAsync(serverHost, port.ToString()); AttemptLogin(userName); IsConnected = true; Task.Run(() => ReadAsync()); } catch (Exception ex) { Debug.LogError(ex.Message); client.Dispose(); client = null; IsConnected = false; } } //接收数据 public async void ReadAsync() { var reader = new DataReader(client.InputStream); try { while (true) { var sizeFieldCount = await reader.LoadAsync(sizeof(uint)); if (sizeFieldCount != sizeof(uint)) { reader.DetachStream(); return; } var stringLength = reader.ReadUInt32(); var actualStringLength = await reader.LoadAsync(stringLength); if (stringLength != actualStringLength) { reader.DetachStream(); return; } var dataArray = new byte[actualStringLength]; reader.ReadBytes(dataArray); var data = Encoding.ASCII.GetString(dataArray); ProcessCommands(data); } } catch (Exception exception) { Debug.LogError(string.Format("Received data: \"{0}\"", "Read stream failed with error: " + exception.Message)); reader.DetachStream(); } }




    • 已编辑 BlueXO 2016年5月10日 9:33
    2016年5月10日 9:31

全部回复

  • 出现了OutOfMemoryException,一般是出现死循环,堆栈空间用完

    while (true) 的方法体放个断点,调试一下


    专注于.NET MIS开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年5月10日 10:06