public void Disconnect() { if (client != null && active) { ClientEventArgs mes = new ClientEventArgs(); mes.message = "Disconnected"; if (get_message != null) { get_message(mes); } client.Close(); client = null; active = false; } }
private void HandleClientComm() { TcpClient tcpClient = client; NetworkStream clientStream = tcpClient.GetStream(); byte[] message = new byte[4096]; int bytesRead; while (active) { bytesRead = 0; try { bytesRead = clientStream.Read(message, 0, 4096); } catch { break; } if (bytesRead == 0) { break; } string read_str = Encoding.UTF8.GetString(message, 0, bytesRead); read_process(read_str); } if (tcpClient != null) { tcpClient.Close(); tcpClient = null; } ClientEventArgs mes = new ClientEventArgs(); mes.message = "Disconnected"; if (get_message != null) { get_message(mes); } active = false; }
public bool Connect() { if (active) { return(true); } try { client = new TcpClient(); IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip), port); active = false; client.Connect(serverEndPoint); active = true; System.Threading.Thread clientThread = new System.Threading.Thread(new System.Threading.ThreadStart(HandleClientComm)); clientThread.Start(); ClientEventArgs mes = new ClientEventArgs(); mes.message = "Connected"; if (get_message != null) { get_message(mes); } return(true); } catch (System.Exception ex) { ClientEventArgs mes = new ClientEventArgs(); mes.message = "Server is not active"; if (get_message != null) { get_message(mes); } client = null; name = ""; active = false; } return(false); }
void time_out_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //time out if (active == false) { client.Close(); client = null; name = ""; active = false; GC.WaitForFullGCComplete(); ClientEventArgs mes = new ClientEventArgs(); mes.message = "server is not active"; if (get_message != null) { get_message(mes); } } time_out_timer.Stop(); time_out_timer.Enabled = false; time_out_timer = null; }
void client_get_message(ClientEventArgs args) { RunOnUiThread(() => { lst_info.Text = lst_info.Text + args.message + "\n"; }); }