void recved(IAsyncResult ar) { try { int BytesRead = socket.EndReceive(ar); if (BytesRead > 0) { byte[] tmp = new byte[BytesRead]; Array.ConstrainedCopy(buffer, 0, tmp, 0, BytesRead); if (bgISocket != null) { bgISocket.receiveData(tmp); } asyncReceive(); } else //对端gracefully关闭一个连接 { disconnect(); } } catch (Exception ex) { disconnect(); } }
public void run() { while (true) { try { int dataLength = 0; dataLength = socket.Available; var dataBuffer = new byte[dataLength]; if (socket != null) { socket.Receive(dataBuffer); } if (dataLength != 0) { bgISocket.receiveData(dataBuffer); } } catch (Exception e) { Debug.LogWarning(e.ToString()); bgISocket.scoketDiconnected(); } Thread.Sleep(10); } }
private void ReceiveAllTime() { if (connected && Connected) { try { int dataLength = 0; dataLength = socket.Available; if (dataLength != 0) { var dataBuffer = new byte[dataLength]; socket.Receive(dataBuffer); bgISocket.receiveData(dataBuffer); } } catch (Exception e) { Debug.LogWarning(e.ToString()); disconnect(); } } }