private bool ServerDisconnected() { _client.Dispose(); _client = null; _isServerConnected = false; return(true); }
private void InternetListen() { while (true) { if (!_flagOfThreadsBreak) { try { bool status = _client != null && _client.Send(new byte[] { }); if (!status) { if (_client != null) { _client.Dispose(); _client = null; } _isServerConnected = false; } } catch { if (_client != null) { _client.Dispose(); _client = null; } _isServerConnected = false; } } Thread.Sleep(1000); } }
private void StartClient() { try { _client = new Tcp_Client(ServerIpAddress.ToString(), ServerPort, ServerConnected, ServerDisconnected, MessageReceived, false); _isServerConnected = true; MessageModel message = new MessageModel(); message.Type = Type.ClientLogon; message.Message = string.Empty; message.PcName = Environment.MachineName; string json = JsonConvert.SerializeObject(message); if (_key == null) { _client.Send(Encoding.UTF8.GetBytes(json)); } else { _client.Send(ExtensionMethods.Encrypt(json, _key)); } } catch { _isServerConnected = false; DisconnectedServer?.Invoke("No server connection"); } }
private bool MessageReceived(byte[] data) { string receiveData = string.Empty; if (_key == null) { receiveData = Encoding.UTF8.GetString(data); } else { receiveData = Encoding.UTF8.GetString(ExtensionMethods.Decrypt(Convert.ToBase64String(data), _key)); } var message = JsonConvert.DeserializeObject <MessageModel>(receiveData); if (message == null) { return(true); } LocalInfo info; info.ipAddress = _client.LocalAddress; info.localEndPoint = Convert.ToInt32(_client.LocalPort); info.pcName = Environment.MachineName; if (message.Type == Type.AcceptLogon) { _isServerConnected = true; ConnectedServer?.Invoke(info); } if (message.Type == Type.Disconnect) { _client.Dispose(); _client = null; _flagOfThreadsBreak = true; _isServerConnected = false; DisconnectedServer?.Invoke("Connection disconnected by Server"); } if (message.Type == Type.Blocked) { _client.Dispose(); _client = null; _isServerConnected = false; DisconnectedServer?.Invoke("It does not connect to the server because it is a blocked IP."); } if (message.Type == Type.Message) { ReceivedMessageFromServer?.Invoke(message.Message); } return(true); }
public void DisconnectServer() { _flagOfThreadsBreak = true; if (_client != null) { _client.Dispose(); _client = null; _isServerConnected = false; DisconnectedServer?.Invoke("No server connection."); } }