服务端:
using System;
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.Data.OracleClient;
using System.Net.Sockets;
using System.Net;
namespace TempAlert
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void tempTimer_Tick(object sender, EventArgs e)
{
DbUtil du = new DbUtil();
StringBuilder sql = new StringBuilder();
sql.Append(" select * from sys_log_absolute where ISSENT = 0");
DataSet ds = du.GetDataSet(sql.ToString());
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 9050);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
if (ds.Tables[0].Rows.Count>0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string bh = ds.Tables[0].Rows[i]["BH"].ToString();
string cgqdate = ds.Tables[0].Rows[i]["CGQDATE"].ToString();
string createdat = ds.Tables[0].Rows[i]["CREATEDAT"].ToString();
string title = ds.Tables[0].Rows[i]["TITLE"].ToString();
string limitTemp = ds.Tables[0].Rows[i]["cgqdate1"].ToString();
string _data = "设备编号:" + bh + "|" + "当前温度:" + cgqdate + "|" + "受限温度:" + limitTemp + "|" + "超温时间:" + createdat + "|" + "报警类型:" + title;
//转换编码成UTF8
Encoding vGBK = Encoding.GetEncoding("GBK");
if (vGBK!=null)
{
byte[] vBytes = vGBK.GetBytes(_data);
string Text = vBytes.Length.ToString();
byte[] vUtf8 = Encoding.Convert(vGBK, Encoding.UTF8, vBytes);
Text = Encoding.UTF8.GetString(vUtf8);
sock.SendTo(System.Text.Encoding.Default.GetBytes(Text), iep2);
}
string updateSql = "update sys_log_absolute set issent = 1 where bh ='"+bh+"'";
du.ExecuteDataBase(updateSql);
}
}
sock.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
tempTimer.Enabled = true;
timer_differ.Enabled = true;
label1.Text = "服务开启";
}
private void button1_Click(object sender, EventArgs e)
{
tempTimer.Enabled = true;
timer_differ.Enabled = true;
label1.Text = "服务开启";
}
private void button2_Click(object sender, EventArgs e)
{
tempTimer.Enabled = false;
timer_differ.Enabled = false;
label1.Text = "服务关闭";
}
private void label2_Click(object sender, EventArgs e)
{
}
private void timer_differ_Tick(object sender, EventArgs e)
{
DbUtil du = new DbUtil();
StringBuilder sql = new StringBuilder();
sql.Append("select * from sys_log_difference where ISSENT = 0");
DataSet ds = du.GetDataSet(sql.ToString());
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 9050);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string bdzid = ds.Tables[0].Rows[i]["bdzid"].ToString();
string name1 = ds.Tables[0].Rows[i]["name1"].ToString();
string maxtemp = ds.Tables[0].Rows[i]["maxtemp"].ToString();
string createdat = ds.Tables[0].Rows[i]["createdat"].ToString();
string title = ds.Tables[0].Rows[i]["TITLE"].ToString();
string limitTemp = ds.Tables[0].Rows[i]["limit_temp"].ToString();
string _data = "变电站号:" + bdzid + "|" + "当前温度:" + maxtemp + "|" + "受限温度:" + limitTemp + "|" + "超温时间:" + createdat + "|" + "报警类型:" + title;
//转换编码成UTF8
Encoding vGBK = Encoding.GetEncoding("GBK");
if (vGBK != null)
{
byte[] vBytes = vGBK.GetBytes(_data);
string Text = vBytes.Length.ToString();
byte[] vUtf8 = Encoding.Convert(vGBK, Encoding.UTF8, vBytes);
Text = Encoding.UTF8.GetString(vUtf8);
sock.SendTo(System.Text.Encoding.Default.GetBytes(Text), iep2);
string updateSql = "update sys_log_difference set issent = 1 where name1 ='" + name1 + "'";
du.ExecuteDataBase(updateSql);
}
}
}
sock.Close();
}
}
}
客户端:
using System;
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.Net.Sockets;
using System.Net;
using System.Threading;
namespace TempAlertClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Socket sock = null;
EndPoint ep = null;
Thread t = null;
private void Form1_Load(object sender, EventArgs e)
{
sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
sock.Bind(iep);
ep = (EndPoint)iep;
Control.CheckForIllegalCrossThreadCalls = false;
t = new Thread(new ThreadStart(fff));
t.IsBackground = true;
t.Start();
}
private void fff() {
while(true){
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
lb_msg.Items.Add(Encoding.Default.GetString(data));
}
}
}
}