//begin accept connect from client and add to dgv private void listener() { _id = 1; try { _tcpListener = new TcpListener(_svrIP, _svrPort); _tcpListener.Start(); lbl_Info.Text = "SERVER - IP: " + _svrIP + "; PORT: " + _svrPort; while (true) { _tcpClient = _tcpListener.AcceptTcpClient(); _clientMng = new clientManager(_id, _tcpClient); dataGridView.Rows.Add(_id, "", "", ""); _clientMng._thread = new Thread(doListen); _clientMng._thread.Start(); _lstClient.Add(_clientMng); Thread.Sleep(1000); _id++; } } catch { int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (error != 10004) { lbl_Info.Text = ""; MessageBox.Show("Lỗi khởi tạo Server!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
//SHUTDOWN private void shutdownToolStripMenuItem1_Click(object sender, EventArgs e) { int id = getId(); if (id != 0) { clientManager client = searchClient(id); if (client != null) { sendMsg("SHUTDOWN|", client._tcpClient); } } else { MessageBox.Show("Chọn máy cần thao tác!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void btnLoadProcess_Click(object sender, EventArgs e) { int id = getId(); if (id != 0) { clientManager client = searchClient(id); if (client != null) { sendMsg("PROCESS|", client._tcpClient); } } else { MessageBox.Show("Chọn máy cần thao tác!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void btnNewProcess_Click(object sender, EventArgs e) { int id = getId(); if (id != 0) { clientManager client = searchClient(id); if (client != null) { string msg = "NEWPROCESS|" + txtNewProcess.Text; sendMsg(msg, client._tcpClient); txtNewProcess.Clear(); } } else { MessageBox.Show("Chọn máy cần thao tác!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void endTaskToolStripMenuItem_Click(object sender, EventArgs e) { { int id = getId(); if (id != 0) { if (listProcess.SelectedItems.Count > 0) { clientManager client = searchClient(id); if (client != null) { sendMsg("ENDTASK|" + listProcess.SelectedItems[0].SubItems[1].Text, client._tcpClient); } } } else { MessageBox.Show("Chọn máy cần thao tác!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
// begin listenner from messager was send by client private void doListen() { TcpClient tcpClient = _tcpClient; clientManager client = _clientMng; NetworkStream netStream = tcpClient.GetStream(); while (true) { try { byte[] buffer = new byte[tcpClient.ReceiveBufferSize]; int x = netStream.Read(buffer, 0, tcpClient.ReceiveBufferSize); int ii = buffer.Length - 1; while (buffer[ii] == 0) { --ii; } byte[] data = new byte[ii + 1]; Array.Copy(buffer, data, ii + 1); try { InfoFromClient = (Info)DeserializeData(data); if (InfoFromClient != null) { LoadProcess(); } else { MessageBox.Show("NA"); } } catch { string receive = Encoding.UTF8.GetString(buffer).Trim(); string[] spl = receive.Split('|'); switch (spl[0].ToUpper()) { case "NAME": for (int i = 0; i < dataGridView.Rows.Count; i++) { if (Equals(dataGridView.Rows[i].Cells[0].Value.ToString(), client._id.ToString())) { dataGridView.Rows[i].Cells[1].Value = spl[1]; break; } } break; case "IP": for (int i = 0; i < dataGridView.Rows.Count; i++) { if (Equals(dataGridView.Rows[i].Cells[0].Value.ToString(), client._id.ToString())) { dataGridView.Rows[i].Cells[2].Value = spl[1]; break; } } break; case "EXIT": for (int i = 0; i < dataGridView.Rows.Count; i++) { if (Equals(dataGridView.Rows[i].Cells[0].Value.ToString(), client._id.ToString())) { string name = dataGridView.Rows[i].Cells[1].Value.ToString(); string ip = dataGridView.Rows[i].Cells[2].Value.ToString(); MessageBox.Show("Máy: " + name + "; Địa chỉ IP: " + ip + " đã ngắt kết nối do người dùng!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Warning); if (String.IsNullOrEmpty(dataGridView.Rows[i].Cells[3].Value.ToString())) { dataGridView.Rows[i].Cells[3].Value = "Đã ngắt kết nối"; } else { dataGridView.Rows[i].Cells[3].Value = dataGridView.Rows[i].Cells[3].Value.ToString() + "; Đã ngắt kết nối"; } break; } } break; } } } catch { client._thread.Abort(); } } }