12
12
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Windows.Forms.DataVisualization.Charting;
using System.Xml;
namespace NguyenThiCuc_Lab10
{
public partial class Form1 : Form
{
string url = "";
Boolean autorequest = false;
string InputData = String.Empty;
delegate void SetTextCallback(string text);
string CommandDataTime = String.Empty;
string dataReceive = string.Empty;
UdpClient clientsock, serversock;
public Form1()
{
InitializeComponent();
timer1.Start();
comboBox1.Items.AddRange(new string[] { "COM", "TCP", "UDP", "HTTP" });
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
if (IsPortInUse(portServer))
{
MessageBox.Show("Port " + portServer + " is already in
use.");
return;
}
Serversock.Bind(iep);
Serversock.Listen(1);
MessageBox.Show("Server TCP đang lắng nghe trên cổng " +
portServer);
while (startServer)
{
try
{
Clientsock = Serversock.Accept();
IPEndPoint Clientep =
(IPEndPoint)Clientsock.RemoteEndPoint;
string ipClient = Clientep.Address.ToString();
int portClient = Clientep.Port;
ipc.Invoke((MethodInvoker)delegate
{
ipc.Text = ipClient + ":" + portClient;
});
while (startServer)
{
try
{
byte[] data = serversock.Receive(ref remoteEP);
string receivedData = Encoding.ASCII.GetString(data);
if (!string.IsNullOrEmpty(receivedData))
{
Invoke((MethodInvoker)delegate
{
getData.AppendText(receivedData +
Environment.NewLine);
});
ipclient = remoteEP.Address.ToString();
int portclient = remoteEP.Port;
Invoke((MethodInvoker)delegate
{
ipc.Text = ipclient + ":" + portclient;
});
ParseAndDisplayData(receivedData);
}
}
catch (Exception ex)
{
MessageBox.Show("Lỗi nhận dữ liệu UDP: " + ex.Message);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Lỗi chung khi chạy acceptClient: " + ex.Message);
}
}
startServer = true;
if (IsPortInUse(portServer))
{
MessageBox.Show("Port " + portServer + " is already in
use.");
return;
}
Serversock.Bind(iep);
Serversock.Listen(1);
MessageBox.Show("Server TCP đã mở thành công!", "Thông báo");
}
else if (comboBox1.SelectedItem.ToString() == "UDP")
{
serversock = new UdpClient(portServer);
remoteEP = new IPEndPoint(IPAddress.Any, portServer);
MessageBox.Show("Server UDP đã mở thành công!", "Thông báo");
}
else
{
MessageBox.Show("Vui lòng chọn giao thức TCP hoặc UDP!", "Lỗi",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
startServer = false;
if (match.Success)
{
float tempValue = float.Parse(match.Groups[1].Value);
float lightValue = float.Parse(match.Groups[2].Value);
string currentTime = DateTime.Now.ToString("yyyy-MM-dd
HH:mm:ss.fff");
if (tb_temp.InvokeRequired)
{
tb_temp.Invoke((MethodInvoker)delegate
{
tb_temp.Text = tempValue.ToString();
tb_light.Text = lightValue.ToString();
dataTable.Rows.Add(countData++, tempValue, lightValue,
currentTime);
});
}
else
{
tb_temp.Text = tempValue.ToString();
tb_light.Text = lightValue.ToString();
dataGridView1.Rows.Add(countData++, tempValue, lightValue,
currentTime);
}
if (chart1.InvokeRequired)
{
chart1.Invoke((MethodInvoker)delegate
{ UpdateChart(currentTime, tempValue, lightValue); });
}
else
{
UpdateChart(currentTime, tempValue, lightValue);
}
}
else
{
MessageBox.Show("Dữ liệu không đúng định dạng!", "Lỗi",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
MessageBox.Show("Lỗi xử lý dữ liệu: " + ex.Message);
}
}