private void Disconnect(ClientRowInfo clientRowInfo) { bool isConnected = true; string result = ""; Bitmap resultImage = null; try { if (clientRowInfo.client != null && clientRowInfo.client.Client != null) { if (clientRowInfo.client.Connected) { clientRowInfo.client.Close(); } clientRowInfo.client.Dispose(); } isConnected = false; resultImage = ParallelTcpClientConnectionApp.Properties.Resources.disconnected; } catch (Exception ex) { result = "Disconnection error!"; resultImage = ParallelTcpClientConnectionApp.Properties.Resources.disconnected; } finally { gvList.Invoke((Action)(() => { gvList.Rows[clientRowInfo.rowIndex].Cells["Status"].Value = resultImage; gvList.Rows[clientRowInfo.rowIndex].Cells["Description"].Value = result; ((DataGridViewDisableButtonCell)gvList.Rows[clientRowInfo.rowIndex].Cells["Send"]).Enabled = isConnected; })); } }
private void SendDataAsync(ClientRowInfo clientRowInfo, string dataText) { lblResult.Invoke((Action)(() => { lblResult.Text = ""; })); try { bool connected = clientRowInfo.client.Client.IsConnected(); if (clientRowInfo.client != null && clientRowInfo.client.Connected) { string result = ""; byte[] byteData = Encoding.UTF8.GetBytes(dataText); NetworkStream networkStream = clientRowInfo.client.GetStream(); { var writer = new StreamWriter(networkStream); { var reader = new StreamReader(networkStream, Encoding.UTF8); { networkStream.Write(byteData, 0, byteData.Length); //result = reader.ReadToEnd(); } } } lblResult.Invoke((Action)(() => { lblResult.ForeColor = Color.DarkGreen; lblResult.Text = result; })); } else { string result = "Client not connected!"; lblResult.Invoke((Action)(() => { lblResult.ForeColor = Color.Red; lblResult.Text = result; })); } } catch (Exception ex) { string result = ex.Message; lblResult.Invoke((Action)(() => { lblResult.ForeColor = Color.Red; lblResult.Text = result; })); } finally { if (clientRowInfo.client != null && !clientRowInfo.client.Connected) { btnSendData.Enabled = false; } } }
private void TcpClient_OnDisconnect(object sender, OnDisconnectEventArgs e) { //e.RowIndex ClientRowInfo clientRowInfo = connectionList.FirstOrDefault(x => x.rowIndex == e.RowIndex); if (clientRowInfo != null) { Disconnect(clientRowInfo); } }
private void gvList_CellContentClick(object sender, DataGridViewCellEventArgs e) { int rowCount = gvList.AllowUserToAddRows ? gvList.Rows.Count - 1: gvList.Rows.Count; if (e.RowIndex < rowCount && e.ColumnIndex == 4 && (gvList.Rows[e.RowIndex].Cells["Send"] as DataGridViewDisableButtonCell).Enabled) { int port; IPAddress ipAddress; gvList.Invoke((Action)(() => { // Ip Address checking if (!IPAddress.TryParse(gvList.Rows[e.RowIndex].Cells["Ip"].Value.ToString(), out ipAddress)) { return; } // Port existing control if (gvList.Rows[e.RowIndex].Cells["Port"].Value == null || string.IsNullOrEmpty(gvList.Rows[e.RowIndex].Cells["Port"].Value.ToString())) { return; } // Port available checking if (!Int32.TryParse(gvList.Rows[e.RowIndex].Cells["Port"].Value.ToString(), out port)) { return; } })); ClientRowInfo clientRowInfo = connectionList.Where(x => x.rowIndex == e.RowIndex).FirstOrDefault(); if (clientRowInfo != null) { SendDataForm sendDataForm = new SendDataForm(clientRowInfo); sendDataForm.ShowDialog(); //string result = SendData(clientRowInfo); //MessageBox.Show(result, "Result"); } } }
private void Connect(ClientRowInfo clientRowInfo) { bool isConnected = false; string result = ""; Bitmap resultImage = null; if (clientRowInfo.client == null) { clientRowInfo.client = new TcpClientEx(clientRowInfo.rowIndex); clientRowInfo.client.OnDisconnect += TcpClient_OnDisconnect; } try { if (clientRowInfo.IpAvailabel && clientRowInfo.PortAvailabel && clientRowInfo.client.ConnectAsync(clientRowInfo.ip, clientRowInfo.port).Wait(5000)) { isConnected = true; resultImage = ParallelTcpClientConnectionApp.Properties.Resources.port_open_32x32; } else { result = "Connection failed"; resultImage = ParallelTcpClientConnectionApp.Properties.Resources.disconnected; } } catch (Exception ex) { result = "Connection error!"; resultImage = ParallelTcpClientConnectionApp.Properties.Resources.disconnected; } finally { gvList.Invoke((Action)(() => { gvList.Rows[clientRowInfo.rowIndex].Cells["Status"].Value = resultImage; gvList.Rows[clientRowInfo.rowIndex].Cells["Description"].Value = result + " / " + isConnected.ToString(); ((DataGridViewDisableButtonCell)gvList.Rows[clientRowInfo.rowIndex].Cells["Send"]).Enabled = isConnected; })); } }
public SendDataForm(ClientRowInfo clientRowInfo) { InitializeComponent(); this.clientRowInfo = clientRowInfo; }