protected override void OnSocketFatalDisconection(Exception e, Socket socket) { Console.WriteLine($"Client '{this[socket].Identifier}' has disconnected"); OnClientDisconnected(this[socket].Identifier); lock (this.LockObj) ClientSockets.Remove(this[socket]); }
private void ReceiveCallBack(IAsyncResult asyncResult) { //Socket socket = (Socket)asyncResult.AsyncState; TCPConnection client = (TCPConnection)asyncResult.AsyncState; if (!client.Socket.Connected) { return; } int receiveBytes = 0; try { receiveBytes = client.Socket.EndReceive(asyncResult); } catch (SocketException sex) { if (sex.SocketErrorCode == SocketError.ConnectionReset) { OnClientClose?.Invoke(client.Socket.RemoteEndPoint); ClientSockets.Remove(client.Socket.RemoteEndPoint); } else { OnException?.Invoke(sex); } } catch (Exception ex) { OnException?.Invoke(ex); return; } if (receiveBytes == 0) { return; } byte[] receiveBuffer = new byte[receiveBytes]; Array.Copy(client.ReceiveBuffer, receiveBuffer, receiveBytes); string receiveData = Encoding.ASCII.GetString(receiveBuffer); try { OnReceive?.Invoke(client.Socket.RemoteEndPoint, receiveData); } catch (Exception ex) { OnException?.Invoke(ex); } }
private async void OnConnection(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args) { Writer = null; ClientSockets.Add(args.Socket); var reader = new DataReader(args.Socket.InputStream); try { while (Working) { var sizeFaildCount = await reader.LoadAsync(sizeof(uint)); var stringLength = reader.ReadUInt32(); var actualStringLength = await reader.LoadAsync(stringLength); if (sizeFaildCount != sizeof(uint) || actualStringLength != stringLength) { reader.DetachStream(); reader.Dispose(); ClientSockets?.Remove(args.Socket); return; } var dataArrary = new byte[actualStringLength]; reader.ReadBytes(dataArrary); var dataJson = Encoding.UTF8.GetString(dataArrary); var data = JsonConvert.DeserializeObject <MessageModel>(dataJson); await SendMsg(data, args.Socket); MsgReceivedAction?.Invoke(data); } } catch (Exception e) { if (SocketError.GetStatus(e.HResult) == SocketErrorStatus.Unknown) { } Debug.WriteLine(string.Format("Received data: \"{0}\"", "Read stream failed with error: " + e.Message)); reader.DetachStream(); reader.Dispose(); ClientSockets?.Remove(args.Socket); } }
private void ReceiveCallback(IAsyncResult ar) { Socket current = (Socket)ar.AsyncState; int received; try { received = current.EndReceive(ar); } catch (SocketException) { // Console.WriteLine("Client forcefully disconnected"); ClientForcefullyDisconnected?.Invoke(); // Don't shutdown because the socket may be disposed and its disconnected anyway. current.Close(); ClientSockets.Remove(current); return; } byte[] recBuf = new byte[received]; Array.Copy(Buffer, recBuf, received); string text = Encoding.ASCII.GetString(recBuf); // Console.WriteLine("Received Text: " + text); if (text.ToLower() == "") { ClientDisconnected?.Invoke(); return; } else { byte[] data = Encoding.ASCII.GetBytes("Received"); current.Send(data); TextReceived?.Invoke(text); } current.BeginReceive(Buffer, 0, BufferSize, SocketFlags.None, ReceiveCallback, current); }